C programs to check whether a string is a palindrome have been shown here. A string is called a palindrome if the same string is obtained when the string is reversed. For example, the string "madam" is a palindrome as we get the same string "madam" while reading from left to right or right to left. But the string "cat" is not a palindrome as we get "tac" after reading the string from right to left.
Here both of the iterative and recursive approaches have been covered. Another approach of string comparison using inbuilt functions like strcmp() and strrev() have also been shown. The algorithm, pseudocode and time complexity of the program have been provided below.
1. Algorithm to check whether a string is a palindrome
1. Take a string $str$ and the size $n$ as inputs.
2. Check for each $i$ from 0 to $n/2 - 1$, if $str[i] = str[n - 1 - i]$.
3. If all the comparisions return true, declare $str$ as a palindrome.
4. Else declare $str$ as not a palindrome.
2. Pseudocode to check whether a string is a palindrome
Input: A string $str$ and size of string $n$
Output: $str$ is a palindrome or not
1. Procedure checkPalindrome($str$, $n$):
2.
3.
4.
5.
6. End Procedure
3. Time complexity to check whether a string is a palindrome
Time Complexity: O(n)
Here $n$ is the length of the string.
4. C Program & output to check whether a string is a palindrome using Iteration
/********************************************** alphabetacoder.com C program to check whether a string is a palindrome or not using iteration **********************************************/ #include <stdio.h> #include <string.h> int main() { // declare character array to store strings char str[30]; // declare variables int i, length, flag = 0; // take input of string printf("Enter the string: "); scanf("%s", str); // calculate the length of string length = strlen(str); // divide the array in two parts // compare charcaters of the left part to the right part // traverse left part from left to right direction // traverse right part from right to left direction for (i = 0; i < (length / 2); i++) { // check for character mismatch if (str[i] != str[length - 1 - i]) { printf("%s is not a palindrome string!\n", str); flag = 1; break; } } // if flag = 0, the string is palindrome if (flag == 0) printf("%s is a palindrome string!\n", str); return 0; }
Output
Case 1:
Enter the string: level
level is a palindrome string!
Case 2:
Enter the string: music
music is not a palindrome string!
5. C Program to check whether a string is a palindrome using strrev( ) and strcmp( ) functions
/********************************************** alphabetacoder.com C program to check whether a string is a palindrome or not using inbuilt functions **********************************************/ #include <stdio.h> #include <string.h> int main() { // declare character arrays to store strings char str[30], rev[30]; // take input of string printf("Enter the string: "); scanf("%s", str); // copy the string str to rev strcpy(rev, str); // reverse the string rev using strrev() strrev(rev); // compare strings str and rev using strcmp() // check if both str and rev are same if (strcmp(str, rev) == 0) printf("%s is a palindrome string!\n", str); else printf("%s is not a palindrome string!\n", str); return 0; }
6. C Program to check whether a string is a palindrome using recursion
/******************************************** alphabetacoder.com C program to check whether a string is a palindrome or not using recursion *********************************************/ #include <stdio.h> #include <string.h> // function to check if a string is a palindrome int check_palindrome(char * s, int i) { // exit condition of recursive call if (i == strlen(s) / 2) return 1; // compare the characters // if mismatch found then return 0 if (s[i] != s[strlen(s) - 1 - i]) return 0; // call function return check_palindrome(s, i + 1); } int main() { // declare character array to store strings char str[30]; // declare variables int length, result; // take input of string printf("Enter the string: "); scanf("%s", str); // calculate the length of string length = strlen(str); // call function to check whether // str is palindrome // function check_palindrome() takes // string str and first charcater index 0 // as parameters result = check_palindrome(str, 0); // if result = 1, the string is palindrome if (result == 1) printf("%s is a palindrome string!\n", str); else printf("%s is not a palindrome string!\n", str); return 0; }