-1

recursive palindrome function this is me solution of the question there a mistack in my solution the not palindrome part is not printing

#include<iostream>
#include<cstring>
using namespace std;

bool ispalindrome( char string1[],int length);
int main()
{
  string word;
  cout<<"Enter a word: ";
  cin>> word;

   if(ispalindrome)
     cout<<"The input word is palindrome";
   else
     cout<<"The input word is NOT palindrome";

   return 0;
    }

  bool ispalindrome( char string1[],int length)
   {

if(length<=1)
    return true;
if ((*string1)==string1[length-1])
return ispalindrome(string1+1, length-2);

else return false;

}

1 Answers1

0

You need to call the method ispalindrome(word, strlen(word)) inside the if statement.

So it should look like this

if (ispalindrome(word, strlen(word)) {
  cout << word << " is a palindrome" << endl;
} else {
  cout << word << " is not a palindrome" << endl;
}

Also, stay consistent with your type. word should be a char array, not a string.

Eli Sadoff
  • 6,628
  • 6
  • 29
  • 54
  • when i intered what you wrote an error showed up,it is in this link http://store2.up-00.com/2016-10/1477696622041.png – sh.alzoubi Oct 28 '16 at 23:20
  • @sh.alzoubi That's why I said you should be consistent with your types. `word` should be declared as `char * word = new char[1024]`, then you won't have any type problems. – Eli Sadoff Oct 29 '16 at 00:36
  • i change word to char, and this is the errors i got http://store2.up-00.com/2016-10/1477749413741.png – sh.alzoubi Oct 29 '16 at 14:02
  • I didn't say `char`. I said `char * word = new char[1024]`. `char` and `char *` are very different. – Eli Sadoff Oct 29 '16 at 14:04