Test a palindrome

I got a problem finish my code. The purpose of the code is to test whether the user input or i may hard code the input is a palindrome. I got the hard part done which is ignore whitespace and punctuation and the testing for the palindrome. However, somehow i can't make it work why?

#include <string>
#include <iostream>

bool IsPalindrome(std::string str){
size_t j = str.length()-1;
size_t i = 0;
for(;i < str.length(); i++)
if((('a'<=str[i]) && (str[i]<='z'))|| (('A'<=str[i]) && (str[i]<='Z')))
break;
for(;j >=0; j-- )
if((('a'<=str[j]) && (str[j]<='z'))|| (('A'<=str[j]) && (str[j]<='Z')))
break;
if(i>=j)
return true;
if(str[i] == str[j])
return IsPalindrome(str.substr(i+1,j-i-1));
else
return false;
}
int main(){
cout<<"please enter a word phrase";
cin<<
if it is a palindrome <-- this is where i have problem, how can i make it to c++
cout<<"it is a palindrome";
else
cout<<"it is not a palindrome";
}
Zhuge (1065) Dec 12, 2010 at 8:48pm
You need to call the function that you wrote on the string you read in.
jimmy5023 (4) Dec 12, 2010 at 9:55pm
Like this?

using namespace std;

int main;
std::string true,userinput;
cout<<"type a word phrase";
cin>>string userinput;
true = IsPalindrome(userinput)?;

AHHH i don't know, it donest' work if i put the one on top in my sequence, what i want is just
If IsPalindrome(userinput) pass all the test
cout<<"this is a palindrome"
else
cout<<"this is not a palindrom";

but somehow i can't successfully implement a if function in my program that works
Hi
Ive got a working example if that will help

In main i have changed cin>>str to getline(cin,str)
because the cin command will only grab the first word and not the entire line
that we need.

In IsPalindrome I add 0x20 (32 decimal) to the uppercase letter to make it lowercase.

And string.append is used to add the current character to our tempString

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <string>
#include <iostream>

using	namespace std;

bool IsPalindrome(std::string str)
{
	/////////////////////////////////////////////////////
	///  Removes Punctuation, spaces, converts to lowercase

	string	tempString;

	for (int i=0, j=0;i<str.length();i++)
	{
		if (str[i] >= 'A' && str[i] <= 'Z')
		{
			tempString.append(1,str[i] + 0x20);		//make Lowercase by adding 0x20
		}
		else if (str[i] >= 'a' && str[i] <= 'z')
		{
			tempString.append(1,str[i]);
		}
	}

	//////////////////////////////////////////////////////
	///	Does Comparison

	int	len = tempString.length();

	for (int i=0;i<len/2;i++)
	{
		if (tempString[i] != tempString[len-i-1])
		{
			return(false);
		}
	}

	return(true);

	
}
int main()
{
	string	strIn;

	cout<<"please enter a word phrase\n";
	getline(cin,strIn);

	if (IsPalindrome(strIn)) 
		cout<<"it is a palindrome";
	else
		cout<<"it is not a palindrome";
}


Hope this helps you.
Shredded
Hey, shredded, why use +0x20, and not do a simple tolower?
Thanks alot.
To: DeusExInfernus

Yeah I didnt even think about using tolower().
But that would be valid too.

Shredded
Topic archived. No new replies allowed.