C + + to determine the number of palindromes

When we learn C ++, we often encounter the problem of letting us judge whether a string is a palindrome string. Below I will show you how to judge.

The meaning of the palindrome string is that reading from back to back and reading from back to front are the same, so we can convert the string to characters to judge.

1 #include <iostream>
 2  using  namespace std;
 3  int main ()
 4  {
 5      string str;
 6      cout << " Please enter a string: " ;
 7      cin >> str;
 8      int sum = 0 ;
 9      int a = str.length ();
 10      if (a == 0 )
 11      {
 12          cout << "The string is an empty string! " << endl;
 13      }
 14     else  if (str.length ()% 2 ! = 0 )
 15      {
 16          for ( int i = 0 ; i <(a- 1 ) / 2 ; i ++ )
 17          {
 18              if (str [i]! = str [a - . 1 - I])
 . 19              {
 20 is                  COUT << " 0 ...... string the string is not a palindrome! " << endl;
 21 is                  BREAK ;
 22 is              }
 23 is              the else 
24                  SUM ++ ;
25          }
 26          if (sum == (a- 1 ) / 2 )
 27              cout << " 1 ...... This string is a palindrome! " << endl;
 28      }
 29      else 
30      {
 31          for ( int i = 0 ; i <a / 2 ; i ++ )
 32          {
 33              if (str [i]! = str [a- 1 - i])
 34              {
 35                  cout << " 0 ...... This string is not a palindrome! " <<endl;
 36                  break ;
 37              }
 38              else 
39                  sum ++ ;
 40          }
 41          if (sum == a / 2 )
 42              cout << " 1 ...... This string is a palindrome! " << endl;
 43      }
 44      return  0 ;
 45 }

The way I judge is to first determine whether the string is an empty string, if it is not an empty string, then determine whether the length of the string is odd or even, and then judge separately.

The idea of ​​this question is still very clear, I hope to help everyone, if it is helpful to everyone, I hope to collect or pay attention, thank you.

Guess you like

Origin www.cnblogs.com/lcy-4/p/12720980.html