C program to check a string is palindrome or not; Through this tutorial, we will learn how to check a string is a palindrome or not using for loop and functions in c programs.
Programs and Algorithm To Check A String Is Palindrome Or Not in C
- Algorithm to Check a Strng is palindrom or Not
- C Program To Check A String Is Palindrome Or Not using For Loop
- C Program To Check A String Is Palindrome Or Not using Function
Algorithm to Check a Strng is palindrom or Not
Use the following algorithm to write to check a whether a given string is palindrome or not; as follows:
- Start Program
- Take a string as input and store it in the array.
- Reverse the string and store it in another array using for loop or function.
- Compare both the arrays.
- Print result
- End Program
C Program To Check A String Is Palindrome Or Not using For Loop
#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; }
The Output of the above c program; as follows:
Enter the string : hello string is not palindrome
C Program To Check A String Is Palindrome Or Not using Function
#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"); }
The Output of the above c program; as follows:
Enter the string: madam string is palindrome