Tutorials, Free Online Tutorials,It Challengers provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, core java, sql, php, c language etc. for beginners and professionals.

Breaking

C Program to Check if a given String is Palindrome

C Program to Check if a given String is Palindrome

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void main()
  5. {
  6.     char string[25], reverse_string[25] = {'\0'};
  7.     int  i, length = 0, flag = 0;
  8.  
  9.     fflush(stdin);
  10.     printf("Enter a string \n");
  11.     gets(string);
  12.     /*  keep going through each character of the string till its end */
  13.     for (i = 0; string[i] != '\0'; i++)
  14.     {
  15.         length++;
  16.     }
  17.     for (i = length - 1; i >= 0; i--)
  18.     {
  19.        reverse_string[length - i - 1] = string[i];
  20.     }
  21.     /*
  22.      * Compare the input string and its reverse. If both are equal
  23.      * then the input string is palindrome.
  24.      */
  25.     for (i = 0; i < length; i++)
  26.     {
  27.         if (reverse_string[i] == string[i])
  28.             flag = 1;
  29.         else
  30.             flag = 0;
  31.     }
  32.     if (flag == 1)
  33.         printf("%s is a palindrome \n", string);
  34.     else
  35.         printf("%s is not a palindrome \n", string);
  36. }

No comments:

Post a Comment