String Palindrome Program in C

«
»

This is a C program to check a given string is palindrome.

Problem Description

This program accepts a string and checks whether a given string is palindrome.

Problem Solution

1. Take a string as input and store it in the array.
2. Reverse the string and store it in another array.
3. Compare both the arrays.

advertisement
Program/Source Code

Here is source code of the C program to check a given string is palindrome. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

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

1. Take a string as input and store it in the array string[].
2. Store the same string into the another array reverse_string[] in the reverse fashion.
3. Using for loop compare the elements of both the arrays.
4. If all the elements of the array are same, then it is a palindrome. Otherwise it is not a palindrome.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
Runtime Test Cases
 
Enter a string
sanfoundry
sanfoundry is not a palindrome
 
 
Enter a string
malayalam
malayalam is a palindrome

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Take C Programming Practice Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
advertisement
If you wish to look at other example programs on Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.