Palindrome

Hello everyone!!

For some reason whenever I enter the word "Rotor" on the first time it detects it as a palindrome just fine. But when I try the word for the third time it says it isnt anymore?

Pls help me Im so confused

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
#include <iostream>
#include <string.h>
using namespace std;

int main(){
    char string1[20], repeat('y');
    int i, length;
    int flag = 0;

do {
    cout << "Enter a string: ";
    cin >> string1;
    
    length = strlen(string1);
    
    for(i=0;i < length ;i++){
        if(tolower(string1[i]) != tolower(string1[length-i-1])){
            flag = 1;
            break;
        }
    }
    
    if (flag) {
        cout << string1 << " is Not a Palindrome\n" << endl; 
    }
    else {
        cout << string1 << " is a Palindrome\n" << endl; 
    }
    
    cout << "Try again [Y]es [N]o > ";
    cin >> repeat;
    cout << endl;
} while (tolower(repeat) == 'y');

    return 0;
}


when I execute it looks like this:
Enter a string: maam
maam is a Palindrome

Try again [Y]es [N]o > Y

Enter a string: test
test is Not a Palindrome

Try again [Y]es [N]o > y

Enter a string: Rotor
Rotor is Not a Palindrome

Try again [Y]es [N]o > n


but when "Rotor" come first it looks like this:
Enter a string: Rotor
Rotor is a Palindrome

Try again [Y]es [N]o > n
Last edited on
You need to reset flag to 0 each time round the do loop - otherwise it has the value from the previous test.
You need to reset flag to 0 each time round the do loop - otherwise it has the value from the previous test.


oHHHHHH I did not know that

tysm for the help seeplus its all good now
there should be check of even case in start
Its fine, for an odd 5 long string its doing this:
[0] ==[4]
[1] ==[3]
[2] ==[2]
[3] ==[1]
[4] ==[0]

and for even 4 length
[0] ==[3]
[1] ==[2]
[2] ==[1]
[3] ==[0]
which works but checking to i = length/2 is better, as it does half the work and ignores the middle letter for odd lengths due to int division.
A quick refactor using C++ strings instead of C strings:
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
#include <iostream>
#include <string>
#include <cstring>

int main()
{
   char again { '\0' };

   do
   {
      std::cout << "Enter a string: ";
      std::string str;
      std::cin >> str;
      std::cout << '\n';

      size_t len   { str.size() };
      bool   isPal { true };

      for (size_t i { }; i < len / 2; ++i)
      {
         if (::tolower(str[i]) != ::tolower(str[len - i - 1]))
         {
            isPal = false;
            break;
         }
      }

      std::cout << str << " is";
      isPal ? (std::cout << "") : (std::cout << " NOT");
      std::cout << " a palindrome.\n\n";

      std::cout << "Try again [Y]es [N]o > ";
      std::cin >> again;
      std::cout << '\n';
   } while (::tolower(again) == 'y');
}
Enter a string: MsMM

MsMM is NOT a palindrome.

Try again [Y]es [N]o > y

Enter a string: Rotor

Rotor is a palindrome.

Try again [Y]es [N]o > n

Getting a more complex sentence from the input stream, such as "Madam, I'm Adam", won't work with std::cin's operator>>. You'd need to use std::getline.
https://www.cplusplus.com/reference/string/string/getline/

If you need to get an entire line of text for a C string, use std::istream::getline.
https://www.cplusplus.com/reference/istream/istream/getline/

Fair warning, mixing getline with operator>> will cause input weirdness to happen. std::istream::operator>> doesn't remove the endline character ('\n') (or specified delimiter) from the input stream, getline does remove the endline character.

There are ways to avoid the issue, one method is to use std::cin >> std::ws:
https://www.learncpp.com/cpp-tutorial/introduction-to-stdstring/

Oh, about that ? operator on line 29, that is the ternary or conditional operator. A short-hand way to replace an if/then block of code.
https://en.cppreference.com/w/cpp/language/operator_other
Last edited on
The tolower() and the other c type functions take an arg of int. When passing a arg of type char then this should be cast to unsigned char. Also for those functions that return a value other than bool, this is again an int which should be cast to char.

See Notes
https://en.cppreference.com/w/cpp/string/byte/tolower
if you want to do phrases, here are some test cases
https://www.youtube.com/watch?v=JUQDzj6R3p4
and for anyone else of a certain age, its worth it.
Last edited on
Topic archived. No new replies allowed.