Write a C program to check whether a string is palindrome or not using inbuilt string library functions. C program to check palindrome string using string library functions.
Example:
Input String: madam
Output: Palindrome string
Required knowledge
Basic C programming, Loop, If else, StringPalindromic string
Palindrome string is a special string which reads same from backward or forward such as madam, mom, eye, dad etc.Logic to check palindrome string
The basic idea behind checking palindrome is if it can be read same from forward and backward then it is palindrome else not. Here in the below algorithm we will traverse the string character by character in both direction at the same time if they are equal then the string is palindrome.Algorithm to check Palindrome string %%Input : text {Array of characters /String) N {Size of the string} Begin: S ← 0; E ← N-1; While (S <= E) do If (text[S] != text[E]) break End if S ← S + 1; E ← E - 1; End while If (S >= E) then write ('String is Palindrome') End if Else then write ('String is Not Palindrome') End if End
Program to check palindrome string
/** * C program to check whether a string is palindrome or not without using string functions */ #include <stdio.h> #define MAX_SIZE 100 //Maximum size of the string int main() { char string[MAX_SIZE]; int len, S, E; /* Reads a string from user */ printf("Enter any string: "); gets(string); /* Finds length of the string */ len = 0; while(string[len]!='\0') { len++; } S = 0; E = len-1; /* Finds if string is equal to its reverse */ while(S <= E) { if(string[S] != string[E]) { break; } S++; E--; } if(S >= E) { printf("String is Palindrome.\n"); } else { printf("String is Not Palindrome.\n"); } return 0; }
Note: You can also use inbuilt string library functions to make the task easier. Using inbuilt string library function you just need to find reverse of the string and then compare it with original string.
Program to check palindrome string using string functions
/** * C program to check whether a string is palindrome or not using string functions */ #include <stdio.h> #include <string.h> #define MAX_SIZE 100 //Maximum size of the string int main() { char string[MAX_SIZE], reverse[MAX_SIZE]; int flag; /* Reads a string from user */ printf("Enter any string: "); gets(string); strcpy(reverse, string); //Copies original string to reverse strrev(reverse); //Finds the reverse of string flag = strcmp(string, reverse); //Checks whether both are equal or not /* If both strings are equal */ if(flag == 0) { printf("String is Palindrome.\n"); } else { printf("String is Not Palindrome.\n"); } return 0; }
Output
Enter any string: madam
String is Palindrome.
String is Palindrome.
Happy coding ;)
You may also like
- String programming exercises and solutions index.
- C program to find length of a string.
- C program to convert uppercase string to lowercase string.
- C program to convert lowercase string to uppercase string
- C program to find total number of vowels and consonants in a string.
- C program to find total number of words in a string.
- C program to find first occurrence of a character in string.
- C program to remove first occurrence of a character from string.
- C program to remove last occurrence of a character from string.
- C program to find frequency of each character in a string.