In this article, we will write a C program to check if a string is a palindrome. A string is said to be palindrome if it remains the same on reading from both ends. It means that when you reverse a given string, it should be the same as the original string. For instance, the string ‘level’ is a palindrome because it remains the same when you read it from the beginning to the end and vice versa. However, the string ‘Simplilearn’ is not a palindrome as the reverse of the string is ‘nraelilpmis,’ which is not the same.
Algorithmic Logic of C Program for String Palindrome
To write a C program for string palindrome, we need to follow the logic below:
- Create a function to check if the string is a palindrome: isPalindrome(str)
- Initialize indexes for low and high levels to be 0 and n-1, respectively
- Until the low index (l) is lower than the high index (h), do the following:
- If str(l) is different from str(h), return false
- If str(l) and str(h) are same, increment l, i.e., l++ and decrement h, i.e., h--
- If we reach this step, it means there is no mismatch, and the string is a palindrome; otherwise, if step 3.A is true, it is not a palindrome.
Let’s use this logic to write a C program to check if the given string is a palindrome.
#include <stdio.h>
#include <string.h>
// Implementing the logic in a function
void isPalindrome(char str[]){
// Initializing indexes
int l = 0;
int h = strlen(str) - 1;
// Step 4 to keep checking the characters until they are same
while (h > l){
if (str[l++] != str[h--]){
printf("%s is not a palindrome string\n", str);
return;
}
}
printf("%s is a palindrome string\n", str);
}
// Driver program
int main(){
isPalindrome("level");
isPalindrome("radar");
isPalindrome("Simplilearn");
return 0;
}
Output:
As you can see in the above output, the strings ‘level’ and ‘radar’ that remain the same after reversing are displayed as a palindrome. However, for this example, we gave the strings ourselves. In the coming examples, we will ask the user to enter the string and write a C program to check a string palindrome using various methods.
C Program for String Palindrome Using the Standard Method
We will write a C program for string palindrome using a standard method without any function or libraries. In this example, we will:
- Read the string entered by the user using gets(s)
- Calculate the length and store it in a variable
- Initialize high and low indexes
- Compare the characters at low and high index
- Use for loop to keep comparing until a mismatch is found
Here’s the implementation of checking a string palindrome using a standard method.
#include <stdio.h>
#include <string.h>
int main(){
char str[1000];
int l,n,comp=0;
printf("Enter the string to check : ");
gets(str);
n=strlen(str);
for(l=0;l<n/2;l++){
if(str[l]==str[n-l-1])
comp++;
}
if(comp==l)
printf("The entered string is a palindrome");
else
printf("The entered string is not a palindrome");
return 0;
}
Output:
Output:
C Program for String Palindrome Using a Function
The logic for writing a C program for string palindrome using a function is almost the same as that while using a standard method. The significant difference is that this time we will create a function for checking palindrome. Also, after reading the string using gets(s), we will pass it as an argument to the function.
Here’s the C program to check if a string is a palindrome using a function.
#include <stdio.h>
#include <string.h>
// Creating the function
int isPalindrome(char *str){
int l,comp=0,n;
n=strlen(str);
for(l=0;l<n/2;l++){
if(str[l]==str[n-l-1])
comp++;
}
if(comp==l)
return 1;
else
return 0;
}
int main(){
char str[1000];
printf("Enter the string to check: ");
gets(str);
if(isPalindrome(str))
printf("The entered string is a palindrome");
else
printf("The entered string is not a palindrome");
return 0;
}
Output:
Output:
C Program for String Palindrome Using Recursion
The algorithm for writing a C program for string palindrome is the same with only a single major difference: the function will keep calling itself recursively until the lower index is not less than half the length of the string, i.e., l<n/2. Since the function will keep calling itself recursively, we will not have to use the for loop this time.
Example
#include <stdio.h>
#include <string.h>
void check(char [], int);
int main(){
char str[15];
printf("Enter a word to check: ");
scanf("%s", str);
isPalindrome(str, 0);
return 0;
}
void isPalindrome(char str[], int l){
int n = strlen(str) - (l + 1);
if (str[l] == str[n]){
if (l + 1 == n || l == n){
printf("The entered word is a palindrome\n");
return;
}
isPalindrome(str, l + 1);
}
else
{
printf("The entered word is not a palindrome\n");
}
}
Output:
Output:
C Program for String Palindrome Using String Library Functions
For this example, we will be using three string library functions defined in the string.h header file: strcpy, strrev, and strcmp. These three functions will allow us to copy, reverse, and compare strings, the three essentials for checking a palindrome string.
Here’s the C program for string palindrome using string library functions.
#include <stdio.h>
#include <string.h>
int main(){
char inArray[100], revArray[100];
printf("Enter the string to check: ");
scanf("%s", inArray);
// Copying input string
strcpy(revArray, inArray);
// Reversing the string
strrev(revArray);
// Comparing the reversed string with input string
if(strcmp(inputArray, reversedArray) == 0 )
printf("%s is a palindrome.\n", inputArray);
else
printf("%s is not a palindrome.\n", inputArray);
getch();
return 0;
}
Output:
Note: The above program uses the strrev() function, which is available only in ANSI C (Turbo C/C++ compilers). Hence, if you are using a standard GCC compiler, the above code might throw an error.
Conclusion
In this article, you have learned how to write a C program for string palindrome in different ways. You can also write it using pointers in C. You can also try writing a program in C++ to check a string palindrome. C++ is an extended version of C programming. If you are new to C++, you can refer to Simplilearn’s C++ Tutorial for Beginners to get a clear understanding of all the fundamentals. Besides that, you can also sign up for our SkillUp platform. The platform offers numerous free courses in different programming languages to improve your skills in any programming language, including C and C++.
You can also go for our Post Graduate Program in Full Stack Web Development, a certification course that provides training in almost 30 programming languages and tools. To put it simply, it can help you get the mastery in multiple development skills required to become a full-stack developer and land an opportunity to work for the biggest companies in the software development world.