recursive palindrome

Pages: 12
so basically (and this is your job) you need to find a way to convert "Able was I ere I saw Elba" to "ablewasiereisawelba" (for example)
sure i will, thanku so much
#include <iostream>
#include <string>
using namespace std;

bool TestPalindrome (string sentence,int first,int last);
bool TestPalindrome (string sentence,int first,int last)
{
string sentance;
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;
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?
1
2
3
bool TestPalindrome (string sentence,int first,int last)
{
    string sentance;

That local variable (which isn't used and shouldn't be there) is hiding the parameter that has the value.
Last edited on
oh yea i got it.
but have nothing to do with the error that m facing it which is i having all the time
that it is palindrome
this ain't prolog
1
2
3
4
5
6
7
int main()
{
   string sentance;
   int last=sentance.length(); //0

   cout<<"Please Write a sentance:";
   cin>>sentance;
but the int last=sentance.length();
last should equal the length of the sentence itself

if i put
int main()
{
string sentance;
int last=sentance.length(); //0

cout<<"Please Write a sentance:";
cin>>sentance;

i will have the output it is a Palindrome and if i put it like this

int main()
{
string sentance;
int last=sentance.length();
cout<<"Please Write a sentance:";
cin>>sentance;
TestPalindrome (sentance,0,last);

it will be it is not a Palindrome also all the time :|
but the int last=sentance.length();
last should equal the length of the sentence itself
Yes,¿what's the length of an empty string?

By the way, cin>>sentance; will read till the first white-space, use getline(cin,sentance); to read a line.
Last edited on
lets assume that it is a word? i got the same error
...
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
Show your updated code and provide test cases.
#include <iostream>
#include <string>
using namespace std;

bool TestPalindrome (string sentence,int first,int last);
bool TestPalindrome (string sentence,int first,int last)
{

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;
}

`last' is out of bounds.
Use `last-1' instead.

Edit: To clarify sentence[last] == '\0'

And learn to post code.
Last edited on
oh thanku so much thats work 100%
Topic archived. No new replies allowed.
Pages: 12