C program to check whether a string is a palindrome or not – In this article, we will detail in on the multiple ways to check whether a string is a palindrome or not in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
Also check list of over 500+ C Programs with source codes.
The ways used in this piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
- Using String Library Function
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
As you can see in the image uploaded above, you need to enter the concerned string first.
The string entered here is ‘alula‘.
As it is clearly visible, ‘alula’ is a palindrome.
Thus, to check the same in C programming is as follows:
Using Standard Method
- If the original string is equal to reverse of that string, then the string is said to be a palindrome.
2)Read the entered string using gets(s).
3) Calculate the string length using string library function strlen(s) and store the length into the variable n.
4) i=0,c=0.
Compare the element at s[i] with the element at s[n-i-1].If both are equal then increase the c value.
Compare the remaining characters by increasing i value up to i<n/2.
5) If the number of characters compared is equal to the number of characters matched then the given string is the palindrome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <stdio.h> #include <string.h> int main() { char s[1000]; int i,n,c=0; printf("Enter the string : "); gets(s); n=strlen(s); for(i=0;i<n/2;i++) { if(s[i]==s[n-i-1]) c++; } if(c==i) printf("string is palindrome"); else printf("string is not palindrome"); return 0; } |
Output:
1 2 3 |
Enter the string: WELCOME before reverse = WELCOME after reverse = EMOCLEW |
Using Function
- The main() function calls the checkpalindrome(char *s) by passing the string as an argument.
2) c=0,calculate the string length n using strlen(s) library function.
3) For loop iterates from i=0 to i<length of the string/2,
If the element at s[i] is equal to element at s[n-i-1] then increase the c value.
4) If the number of elements compared is equal to the number of elements matched then the function return 1, otherwise it returns 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> #include <string.h> int checkpalindrome(char *s) { int i,c=0,n; n=strlen(s); for(i=0;i<n/2;i++) { if(s[i]==s[n-i-1]) c++; } if(c==i) return 1; else return 0; } int main() { char s[1000]; printf("Enter the string: "); gets(s); if(checkpalindrome(s)) printf("string is palindrome"); else printf("string is not palindrome"); } |
Output:
1 2 |
Enter the string: madam string is palindrome |
Using Recursion
- The main() calls the function checkpalindrome(char *s).
2) The function checkpalindrome(char * s)i=0,c=0.Calculate the string length n using strlen(s).
a) i=0,c=0.Calculate the string length n using strlen(s).
b) if i<length of the string/2
If the element at s[i] is equal to the element at s[n-i-1] then increase the c value and i value.
The function calls itself.
The function calls itself recursively until i<n/2.
c) If i!<length of the string/2,
If i=c then this function returns 1 otherwise it returns 0.
3) If the returned value is 1 then print the given string is a palindrome. If the returned value is 0 then print the string is not a palindrome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <stdio.h> #include <string.h> int checkpalindrome(char *s) { static int i,c=0,n=strlen(s); if(i<n/2) { if(s[i]==s[n-i-1]) c++; i++; checkpalindrome(s); } else { if(c==i) return 1; else return 0; } } int main() { char s[1000]; printf("Enter the string: "); gets(s); if(checkpalindrome(s)) printf("string is palindrome"); else printf("string is not palindrome"); } |
Output:
1 2 |
Enter the string: alamala string is palindrome |
Using String Library Function
- Read the string s using gets(s1) and store into the variable s1.
Using library functions we are comparing the two strings.The string library functions are available at string.h header file.
2) Copy the string s1 to s2 using the library function strcpy(s2,s1).
3) Reverse the string s2 using the library function strsev(s2).
4) Then compare the two strings s1 and s2 using the library function strcmp(s1,s2).
5) If the function strcmp(s1,s2) returns 0 then print the string is palindrome.Otherwise, print the string is not a palindrome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> #include <string.h> int main() { char s1[1000],s2[1000]; printf("Enter the string: "); gets(s1); strcpy(s2,s1); strrev(s2); if(!strcmp(s1,s2)) printf("string is palindrome"); else printf("string is not palindrome"); return 0; } |
1 2 |
Enter the string: alula string is palindrome |