what is wrong with my code?
in inputs like "radar" or "tttt" should say its palindrome.

#include <iostream>
using namespace std;

bool testpalindrome(string,int,int);

int main()
{
    string wordX;
    bool result;

    cout<<"Enter string: ";
    getline(cin,wordX);

    result=testpalindrome(wordX,wordX.length()-1,0);

    result==true ? cout<<"\nthe string is palindrome!" : cout<<"\nthe string is NOT palindrome!\n";
}

bool testpalindrome(string word,int last,int start)
{
    if (last==0)
    {
        return true;
    }
    else
    {
        if(word[start]!=word[last])
            return false;
        else
            testpalindrome(word,last-1,start+1);
    }
}

Recommended Answers

All 8 Replies

Hello ntrncx,
I think there is something wrong here testpalindrome(word,last-1,start+1); It should be last-- and start++ because last and start do not decrement or increment in last-1 or start+1

if at line 15 i put

cout<<boolalpha<<result;

to see if result changes,it does...but dont know where the problem is.
i changed tot -- and ++ but the same...

Try --last or ++start because i think start++ will first send the value to the funciton and then increment.

i dont think its that cause at start in word radar compares 0 with 4 if i preincrement will be 0 with 3,plus the result changes,as i said.

After comparing the first and last letter, the function should compare the second letter and the last before letter,so on. You should preincrement start and predecrement last.

radar
0 r
1 a
2 d
3 a
4 r

so must be 0-4 then 1-3 or i am wrong?
anyway i tried what you said since i have nothing to loose but nothing again
read about what i say... about result swaping to true or false depending the situation just prints all the time not palindrome

Yes, It must be 0-4,1-3.

solved i figured it alone thanks anyway

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.