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

This C Program checks a given string is palindrome without using the Built-in Function. This program first performs reverse of a string. Then it checks whether the given string is equivalent to the reversed string. If they are equal then the string is called as palindrome. The program doesn’t use any built-in function.

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.  * C program to find the length of a string without using the
  3.  * built-in function also check whether it is a palindrome
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. void main()
  9. {
  10.     char string[25], reverse_string[25] = {'\0'};
  11.     int i, length = 0, flag = 0;
  12.  
  13.     printf("Enter a string \n");
  14.     gets(string);
  15.     /*  keep going through each character of the string till its end */
  16.     for (i = 0; string[i] != '\0'; i++)
  17.     {
  18.         length++;
  19.     }
  20.     printf("The length of the string '%s' = %d\n", string, length);
  21.     for (i = length - 1; i >= 0 ; i--)
  22.     {
  23.         reverse_string[length - i - 1] = string[i];
  24.     }
  25.    /*  Check if the string is a Palindrome */
  26.  
  27.     for (flag = 1, i = 0; i < length ; i++)
  28.     {
  29.         if (reverse_string[i] != string[i])
  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. }


$ cc pgm53.c
$ a.out
Enter a string
how  are you
The length of the string 'how  are you' = 12
how  are you is not a palindrome
 
$ a.out
Enter a string
madam
The length of the string 'madam' = 5
madam is a palindrome
 
$ a.out
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 Reference Books in C Programming, Data-Structures and Algorithms

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.
Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Subscribe Newsletter & Posts
Follow Manish & Sanfoundry