0

I'm writing a program to check whether a string is palindrome or not using recursion.Palindrome string is the one that can be read backwards just the same as reading it forward. However following is my code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int num;
printf("Enter the number of characters in your string\n");
scanf("%d",&num);
char string[num];
char string2[num];
int i;
printf("Enter your string\n");
for (i=0;i<num;i++)
{
    scanf("%c",&string[i]);
}
fillString(string,string2,num);
int palin = isPalind(string,string2,num);
if (palin) printf("The string you entered is palindrome");
else printf("The string you entered is not palindrome");

return 0;
}

int isPalind(char string[],char string2[],int num)
{
int i=0;
while (i<num)
{
    if (string[i]!=string2[i])
    {
        i++;
        return 0;

    }
    else{

        i++;
        return 1*isPalind(string,string2,num);

    }
}

}

void fillString(char string[],char string2[],int num)
{
int i;
for (i=1;i<num;i++)
    string2[i-1]=string[num-i];

}

I have a logical error, the program compiles fine and executes but it always gives out "The string is not palindrome"

Raya Rateb
  • 65
  • 1
  • 8
  • why do you call `return 1*isPalind(string,string2,num);`? – wimh Mar 27 '15 at 20:00
  • First I enter the number of characters in the string then I enter the string, but whatever my string is it will always give me "The string is not palindrome" – Raya Rateb Mar 27 '15 at 20:03
  • @Wimmel this is recursion so it will keep executing until the counter is equal to the number or until it finds out that the string is not palindrome(from the result "0") – Raya Rateb Mar 27 '15 at 20:05
  • @RayaRateb but in each recursive call you set i back to 0, and only compare the first character. – wimh Mar 27 '15 at 20:07
  • @RayaRateb btw, the problem is probably your `scanf`, see [Why scanf(“%d”, …) does not consume '\n'?](http://stackoverflow.com/q/13275417/33499) – wimh Mar 27 '15 at 20:08

3 Answers3

1

In the fillString the loop is iterating num-1 times (i is from 1 to num-1), so not the whole string is copied. The first character of the original string is omitted. You should do

for (i=1;i<=num;i++) ...

As for the recursive function, it is not really recursive. In recursive call modified input should be passed, but in your case exactly the same input is passed. So in case of true palindrome, it's likely you will get stack overflow due to non-termination. I would propose another approach, to work with single string recursively:
1) Base case: String is a palindrome if of length 0 or 1
2) Recursion step: String is a palindrome if the first character equals to the last and the string without first and last characters is palindrome.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
0

The both functions are wrong. They can be written the following way

int isPalind( const char string[], const char string2[], int num )
{
    return ( num == 0 ) || 
           ( string[0] == string[--num] && isPalind( string + 1, string2, num ) ); 

}

void fillString( const char string[], char string2[], int num )
{
    int i;
    for ( i = 0; i < num; i++ ) string2[i] = string[num-i-1];
}

If you need not necessary a recursive function then you could use simply standard function memcmp that to determine whether two strings are equal.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Is your fillString() function reversing your string as expected? It looks like the first letter of string1 is not getting added into the last position of string2 because the for loop stops when i < num fails. Try switching it to i <= num and seeing if that helps.

Double-check this example:

Given: String1 = "Hello". String2 = null for now. num = 5.

void fillString(char string[],char string2[],int num)
{
int i;
for (i=1;i<num;i++)
    string2[i-1]=string[num-i];

}

When i = 4, you have string2 = 'olle'. When i = 5, the for loop condition fails, so it doesn't populate string2[4] = 'H'.

updated:

void fillString(char string[],char string2[],int num)
{
int i;
for (i=1;i<=num;i++)
    string2[i-1]=string[num-i];

}
Will
  • 1
  • 2