Bool function

A palindrome is a string that is equal to its reverse - "mom", "I", "radar", and "able was I ere I saw elba". Write a recursive Boolean function that determines whether its parameter is a palindrome. You should ignore whitespace and punctuation in the parameter. You may simply hard code the text that is then initially passed to the function.
So here's what i wrote


// string::length
#include <iostream>
#include <string>
using namespace std;

int main ()
{

function bool isPalindrome(String input)
{
int len = input.length();

bool isPalindrome = true;


// only have to iterate through half the length of the string
for(j = 0; j<len/2; j++)
{
if(input[j] != input[len - 1 -i])
{
isPalindrome = false;
break;

}
}

return isPalindrome;
}



}


But it's not working why!!?
if(input[j] != input[len - 1 -i]) // <--- what's i? (supposed to be j I guess)
[code] Your code goes here [/code]
That is not recursive.
You can't define a function inside another function.
The language(?) is case sensitive String string

What is not working? please be more specific
1. Please write your code between [ code][/code ] brackets.
2. The ispalindrome function should be defined outside of the main function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

bool ispalindrome (string);

int main()
{
    string s;
    ispalindrome(s);
    //rest of code
}

bool ispalindrome(string s)
{
      //code
}


3. for(j = 0; j<len/2; j++) should be for(int j = 0; j<(len/2); j++)
4.
if(input[j] != input[len - 1 -i])
should be if(input[j] != input[len - 1 -j])


Last edited on
Topic archived. No new replies allowed.