📜  回文串程序中的 - C 编程语言代码示例

📅  最后修改于: 2022-03-11 15:04:37.349000             🧑  作者: Mango

代码示例1
#include 
#include 
int main()
{

     char str[80];
     int length;
     printf("\nEnter a string to check if it's a palindrome: ");
     scanf("%s", str);     // string input
     length = strlen(str); // finding the string length
     int i, j, count;
     for (i = 0, j = length - 1, count = 0; i < length; i++, j--)
     {
          // matching string first character with the string last character
          if (str[i] == str[j])
          {
               count++; // if character match, increasing count by one.
          }
     }
     if (count == length) // if count == length, then the string is a palindrome.
     {
          printf("'%s' is a palindrome.\n", str);
     }
     else // otherwise the string is not a palindrome.
     {
          printf("'%s' is not a palindrome.\n", str);
     }
     return 0;
}