checking for palidrone words

hey guys am trying to write a code to check if words are palindrone or not basically words like madam,level,radar,racecar..etc words dat have same meaning wen read backwards...my function kinda works by checking the last character with first, the second last character with de second until all characters are checked..bt am getting am getting "is palidrone" for every check that is correct and every incorrect check "is not palidrone" repeated...please help

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
31
 #include <iostream>
#include <string>

using namespace std;


int main()
{
  string text;
  cout<<"please enter string"<<endl;
  cin>>text;


  int j=text.size();
  int k=1;
  for(int i=0;i<=j-k;i++)
  {
      if(text.at(i)==text.at(j-k))
      {
          cout<<"text is palendrone"<<endl;
      }
      else
      {
        cout<<"text is not palendrone"<<endl;
      }
      k++;
  }


}
Hi,

Well you have your std::cout inside your for loop, so that will output for every character.

The word is only a palindrome if all the characters compare, so that decision needs to be made after the end of the for loop.

However, if 2 chars don't compare, then it's not a palindrome, you can break out of the loop early.

Have you used a bool type of variable yet? It is a type that stores either true or false

Good Luck :+)



thanx allot il try ur way
is dis how u use bool function coz if it is its only returning positive even wen word is not palidrome....am guessing my implimentation is wrong.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  
#include <iostream>
#include <string>

using namespace std;
bool isPalidrome(const string&text)
{
    int str_size=text.size();
    int k=1;
    for(int i=0;i<=(str_size-k);i++)
    {
        if(text.at(i)==text.at(str_size-k))
        {
            return true;
        }
        else
        {
            return false;
        }
        k++;


    }


}

int main()
{
    string word;
    cout<<"please enter text"<<endl;
    cin>>word;
    isPalidrome(word);
    if(true)
    {
        cout<<"positive"<<endl;
    }
    else if(false)
    {
       cout<<"negative"<<endl;
    }

}



That still isn't quite right. You always return after comparing the first and last characters. If the characters are NOT equal, then you can return false. Otherwise you have to continue comparing. Only if you make all the way through the string can you return true, so put the "return true" after the loop.
okay i did wat u said and made some few changes as well but still get positive
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
31
32
33
34
35
36
37
38
39
40
41
42
 

#include <iostream>
#include <string>

using namespace std;
bool isPalidrome(const string&text)
{
    int str_size=text.size();
    int k=1;
    for(int i=0; i<=(str_size-k); i++)
    {
        if(text.at(i)!=text.at(str_size-k))
        {
           return false;
        }
        k++;


    }
    return true;


}

int main()
{
    string text;
    cout<<"please enter text"<<endl;
    cin>>text;
    isPalidrome(text);
    if(true)
    {
        cout<<"positive"<<endl;
    }
    else if(false)
    {
        cout<<"negative"<<endl;
    }

}
Line 32: How is that ever going to be false?

Try this:
1
2
   if (isPalidrome(text))
...

thanx it works nycly
Topic archived. No new replies allowed.