Discussion Locked

Program using recurssion

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Please send me the program to find the whether the given string is palindrome or not using the recursion in C Language.

9 replies

by cecosr

...
s="anitalavalatina";
m=(strlen(p)%2?strlen(p)/2:strlen(p)/2-1);
l=strlen(p)+1;
r=pal(1,x,l,s);
printf("word is %s",(r?"palindrome":"not palindrome"));
...

int pal(int i,int m,int l,char *s){
if(i<=m)
if(s[i]==s[l-i])
i=pal(i++,m,l,s);
else
i=0;
return(i);
}

by lastorck

bool pal(const char* str)
{
static int i = 0;
int len = strlen(str)-1-i;
if(str[0] != str[len])
{
i = 0;
return false;
}
if(i == len || i+1 == len)
{
i = 0;
return true;
}
i++;
return pal(str);
}

//or so better
bool pal(const char* str)
{
const char *fv = str,
*bv = str+strlen(str)-1;
while(fv++ <= bv--)
if(*fv != *bv)
return false;
return true;
}

by shadowtraitor

#include
#include
#include

int index = 0;
int counter;
char Input[10];// the original string entered by the user
char ReInput[10]; // the original user input string reversed

int Palindrome(char Original[], char Reverse[], int index, int count);

void main()
{
printf("Enter string you wish to check no more than 10 Characters in lenght\n");
gets(Input);
counter = strlen(Input);
for (int i = counter - 1; i >= 0 ; i--)
{
ReInput[index++] = Input[i];//reverses the string
}

if (Palindrome(Input,ReInput,0,counter) == 1 )
printf("The String You entered is a Palindrome");
else
printf("The String You entered is not a Palindrome");

getch();}

/* Function used to determine if a string is a palindrome or not, this is done using recursion
Accepts: Original: the string entered by user
Reverse: the user input string reversed
Index: counter used to determine the position in the string being examined
count: the overall lenght of the strings being cheked
Returns: 1 if string is a palindrome
0 if string is not a palindrome
*/
int Palindrome(char Original[], char Reverse[], int index, int count)
{
if (Original[index] != Reverse[index])// compare the letters in the same postion in both the original string and the reverse string
return 0; //the letters are not the same so the string is not a palindrome
if (index == count - 1)// check if you have come to the end of the strings being checked
return 1;// you have reached the end of the strings and they both contain the same word, so string is a palindrome
else
return (Palindrome(Input,ReInput,index + 1,count));//you have not checked the whole string as yet move on to the next letter in the strings, done by index + 1
}

This discussion is closed to new replies.