Bool Function

Cannot find what is wrong, please help
create a bool function to check the entered word is whether a palindrome.

Thanks for your help in advance.


#include <iostream>
#include <string>
using namespace std;

bool isPalindrome(string str);

int main()
{
string str;
cout<<"Enter word :";
cin>>str;

isPalindrome(str);

if (isPalindrome)
cout<<"The word is a palindrome.\n";
else
cout<<"The word is not a palindrome.\n";
cout<<endl;

return 0;

}

bool isPalindrome (string str)
{
int i;
int length=str.length();
int j=length;

for (i=0; i<j; i++)

if (str.at(i)=!str.at(j-1))
return false;

if (str.at(i)==str.at(j-1))
i++;
j--;
return true;
}
You don't state what is wrong; however, your for-loop in isPalindrome() should encase the five or so lines below its start. And please use the code tags, which are found under Format.
Firstly :

1
2
3
4
5
6
cout<<"Enter word :";
cin>>str;
isPalindrome(str);

if (isPalindrome(str))
cout<<"The word is a palindrome.\n";


You forgot to call the function. Or you don't know how to call a function properly.
when I enter word "hello" it came out that is not true.
also
when I enter word "kayak (which is palindrome)" it came out that is not true too.

I guess something wrong within the bool isPalindrome function.
1
2
3
4
5
6
7
8
9
10
bool isPalindrome (string str)
{
      int i;
      int length = str.length();

      for (i = 0; i < length; ++i)
            if (str.at(i) != str.at(length - i - 1)) return false;

      return true;
}


You might want to google to learn more about how this algorithm works
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

bool isPalindrome(const std::string );

int main()
{
    std::string str;
    std::cout << "Enter word: ";
    std::cin >> str;
    
    if ( isPalindrome(str) )
        std::cout<<"The word is a palindrome.\n";
    else
        std::cout<<"The word is not a palindrome.\n";

    return 0;
}

??? isPalindrome (???)
{
    int ??? = ???;

    for ( int i = 0; i < length / ???; i++)
    {
        if ( ??? != ??? )
        return ???;
    }   
    return ???;
}
Thank you Half Life G Man,

You saved me from the last few days to solve this bool function.

Topic archived. No new replies allowed.