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