C Program to Check whether a given String is Palindrome or not using Recursion

Advertisements
The following C program, with recursion, determines whether the entered string is a palindrome or not. A palindrome is a word, phrase or sentence that reads the same backward or forward.

Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Check whether a given String is Palindrome or not 
  3.  * using Recursion
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. void check(char [], int);
  9.  
  10. int main()
  11. {
  12.     char word[15];
  13.  
  14.     printf("Enter a word to check if it is a palindrome\n");
  15.     scanf("%s", word);
  16.     check(word, 0);
  17.  
  18.     return 0;
  19. }
  20.  
  21. void check(char word[], int index)
  22. {
  23.     int len = strlen(word) - (index + 1);
  24.     if (word[index] == word[len])
  25.     {
  26.         if (index + 1 == len || index == len)
  27.         {
  28.             printf("The entered word is a palindrome\n");
  29.             return;
  30.         }
  31.         check(word, index + 1);
  32.     }
  33.     else
  34.     {
  35.         printf("The entered word is not a palindrome\n");
  36.     }
  37. }}


$ gcc palindrome.c -o palindrome
$ a.out
Enter a word to check if it is a palindrome
malayalam
The entered word 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.
Advertisements
Subscribe Newsletter & Posts

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.

Practice Interview Questions & Answers, Quizzes, Objective, Multiple Choice Aptitude Tests for Freshers and Experienced People in these topics - Learn C, C++, Java and C# Programming with coding example on Simple problems as well as tough Algorithms and Data-structures along with runtime output: - C Programs, Data-Structures and Algorithms, C++ Algorithms, Java Algorithms, C# Programs, Android Programs in Java

C Tutorials and Linux Commands Tutorial - C Tutorials, Linux Commands

Career Mentoring with our Founder / CTO - Read More
Advertisements

Follow Manish & Sanfoundry