int main()
{
string sentance;
int last=sentance.length();
cout<<"Please Write a sentance:";
cin>>sentance;
TestPalindrome (sentance,0,last);
if (TestPalindrome(sentance,0,last))
cout<<"It is a Palindrome"<<endl;
else
cout<<"It is not a palindrome"<<endl;
return 0;
}
this code is working but all the time gave me that it is a palindrome idk why ?
any help or suggestion?
...
An string starts empty, you try to calculate the length of an empty string. After you calculate the length of the empty string, you read your word.
The length will not update automagically.
You need to read the string, and after that compute its length
1 2 3
string sentence;
getline(cin,sentence);
int last = sentence.length();
i got that but the problem not in that..
let assume that it is a word not a sentence still m get either all the time Palindrome or all the time it is not Palindrome
if (last<=1)
return true;
else
if (sentence[first]==sentence[last])
return TestPalindrome(sentence,first+1,last-1);
else return false;
}
int main()
{
string sentance;
getline (cin, sentance);
int last=sentance.length();
TestPalindrome (sentance,0,last);
if (TestPalindrome(sentance,0,last))
cout<<"It is a Palindrome"<<endl;
else
cout<<"It is not a palindrome"<<endl;
return 0;
}