About recursion palindrome

In my function palindrome, i just successful to compare 1st char and last char only..
How do i compare the second char which is(A) and second last char which is (a)??



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 #include <ctype.h>
using namespace std;

int palindrome(char *p1, char *p2){

 if ((tolower(*p1) == tolower(*p2)))
    return true;

  else
    return false;
}

int main(){

char str[] = "MAdam";

int palindrome(char *p1, char *p2);

   if(palindrome(str, str+strlen(str)-1))
     cout << str << " is a palindrome" << endl;

   else
     cout << str << " is not a palindrome" << endl;

return 0;
}
You never actually make a recursive call.
To be recursive, palindrome must call itself.

4
5
6
7
8
int palindrome (char *p1, char *p2)
{   if (tolower(*p1) != tolower(*p2))
        return false;  // Not a palindrome
    return palindrome (p1+1, p2-1);  // check next pair of characters
}


Line 17: function prototypes should normally be in the global scope, not local scope. In this case, you don't need line 17 at all since palindrome precedes main().

Last edited on
Topic archived. No new replies allowed.