C Program to Check if a String is a Palindrome without using Built-in Function

This is a C program to check a given string is palindrome without using the Built-in Function.

Problem Description

This program accepts a string and checks whether a given string is palindrome without using the built-in function.

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.

Program/Source Code

Here is source code of the C program to check a given string is palindrome without using the Built-in Function .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 find the length of a string without using the
  4.  * built-in function also check whether it is a palindrome
  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.     printf("Enter a string \n");
  15.     gets(string);
  16.     /*  keep going through each character of the string till its end */
  17.     for (i = 0; string[i] != '\0'; i++)
  18.     {
  19.         length++;
  20.     }
  21.     printf("The length of the string '%s' = %d\n", string, length);
  22.     for (i = length - 1; i >= 0 ; i--)
  23.     {
  24.         reverse_string[length - i - 1] = string[i];
  25.     }
  26.    /*  Check if the string is a Palindrome */
  27.  
  28.     for (flag = 1, i = 0; i < length ; i++)
  29.     {
  30.         if (reverse_string[i] != string[i])
  31.             flag = 0;
  32.     }
  33.     if (flag == 1)
  34.        printf ("%s is a palindrome \n", string);
  35.     else
  36.        printf("%s is not a palindrome \n", string);
  37. }
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.

advertisement
advertisement
Runtime Test Cases
 
Enter a string
how  are you
The length of the string 'how  are you' = 12
how  are you is not a palindrome
 
 
Enter a string
madam
The length of the string 'madam' = 5
madam is a palindrome
 
 
Enter a string
mam
The length of the string 'mam' = 3
mam 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

Note: Join free Sanfoundry classes at Telegram or Youtube
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.

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.