A string that reads the same a forwards and backwards is known as palindrome.
Like MADAM, RADAR, STATS, SOLOS, TENET, WOW
Here we will see programs to check whether a string is palindrome or not.
We use gcc 9.1.0 compiler to run our programs
We will discuss Palindrome program as below
- Simple program using loop
- Program using string reverse function
- Functions to check palindrome
- Recursion to check palindrome
1 Programs to check palindrome in C String
- Create a character array
char str[100]
- Take input from user
scanf("%s", str)
- Get String length
strlen(str)
- match starting and ending characters by increasing loop counter
for (i = 0; i <= length / 2; i++)
up to mid of the string. - Take one one character from start and end.
- From string starting take character in increasing order
- From string ending take character in decresing order
- match both characters
if (str[i] == str[length - 1 - i])
- initialize counter to check each starting and ending characters are mating or not
count++
- if loop variable and count variable is equal means all starting and ending characters are matching
if (count == i)
- Display the result based on match or mismatch.
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 | #include <stdio.h> #include <string.h> int main() { char str[100]; int length, i, count = 0; printf("Enter a string: "); scanf("%s", str); length = strlen(str); for (i = 0; i <= length / 2; i++) { if (str[i] == str[length - 1 - i]) { count++; } } if (count == i) { printf("String %s is palindrome", str); } else { printf("String %s is not palindrome", str); } return 0; } |
Output
1 2 3 4 5 | Enter a string: MADAM String MADAM is palindrome -------------------------------- Enter a string: HELLO String HELLO is not palindrome |
2. Palindrome string program in c using string functions
program to check whether a string is palindrome or not can be also done using functions.
GCC 9.1.0 does not provide reverse functions.
Other compiler provides strrev() function.
We have created our own String reverse function to reverse the string.
Steps for checking string is palindrome or not
- Get String from user
scanf("%s", str)
- Reverse the string using
reverse(str, str1)
- Reverse string is stored in
str1
variable - Compare original and reversed string
strcmp(str, str1)
- If string comparison is zero then string is palindrome else not.
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 | #include <stdio.h> #include <stdlib.h> #include <string.h> char * reverse(char * str, char * str1) { int i; int length = strlen(str); for (i = 0; i < length; i++) { str1[length - 1 - i] = str[i]; } return str1; } int main() { char * str = malloc(100); char * str1 = malloc(100); int length, i, count = 0; printf("Enter a string: "); scanf("%s", str); reverse(str, str1); int c = strcmp(str, str1); if (c == 0) { printf("String %s is palindrome", str); } else { printf("String %s is not palindrome", str); } return 0; } |
3 Function to check String is Palindrome or not
Based on our first program we created a function checkPalindrome()
that is used to check string is palindrome or not.
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 | #include <stdio.h> #include <string.h> int checkPalindrome(char * str1) { int i, check = 0, count = 0; int length = strlen(str1); for (i = 0; i <= length / 2; i++) { if (str1[i] == str1[length - 1 - i]) { count++; } } if (count == i) { check = 1; } return check; } int main() { char str[100]; printf("Enter a string: "); scanf("%s", str); if (checkPalindrome(str)) { printf("String %s is palindrome", str); } else { printf("String %s is not palindrome", str); } return 0; } |
We created this function based on our second program.
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 | #include <stdio.h> #include <stdlib.h> #include <string.h> int checkPalindrome(char * str, char * str1) { int i; int length = strlen(str); for (i = 0; i < length; i++) { str1[length - 1 - i] = str[i]; } int c = strcmp(str, str1); return c; } int main() { char * str = malloc(100); char * str1 = malloc(100); int length, i, count = 0; printf("Enter a string: "); scanf("%s", str); int c = checkPalindrome(str, str1); if (c == 0) { printf("String %s is palindrome", str); } else { printf("String %s is not palindrome", str); } return 0; } |
Recursion to check string palindrome
Recursion can also be used to check string palindrome.
- Create a function palindrome which takes character pointer as argument.
- find the length of string pointed by str.
- check condition if count
variable i< length/2
then do step4 to step 6 - Match starting and ending character if matched increase variable
c
value - increase the counter variable i value
- call again the same function with increasing base function
palindrome(str + 1)
- if condition 3 is false again check if variable i and c are equal
(c == i)
- if both are equal then string is palindrome so return 1. else return 0
variable i and c are declared as static because we have to track these value across multiple palindrome()
function.
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 | #include <stdio.h> #include <string.h> int palindrome(char * str) { static int i = 0, c = 0; int length = strlen(str); if (i < length / 2) { if (str[i] == str[length - i - 1]) { c++; } i++; palindrome(str + 1); } else { if (c == i) { return 1; } else { return 0; } } } int main() { char str[100]; int p; printf("Enter a string: "); scanf("%s", str); if (p == 0) { printf("String %s is palindrome", str); } else { printf("String %s is not palindrome", str); } return 0; } |