
Read a string, Store it in an array and check whether it is palindrome word or not in C
Algorithm
Input: Input a string (Word)
output: Checks if it is a palindrome or not
- Start
- n = 0
- Read a string to check if its palindrome and store it in an array say str
- l = strlen(str)
- i = 0
- while ( i <l/2 )
6.1 if str[i] != str[l-1-i]
6.1.1 n = n+1
6.2 i++ - if n == 0
7.1 Print "It is a palindrome" - else
8.1 Print "It is not a palindrome" - Stop
C Program
Note: gets() is a deprecated, so the following code may not work on your compiler.
#include <stdio.h>
#include <string.h>
int main()
{
int n = 0,l;
char str[30];
printf("Enter a word to check palindrome: ");
gets(str);
l = strlen(str); //Size of the inputed string.
// Checking for Palindrome
for(int i = 0; i <=l/2; i++ ) {
if (str[i] != str[l-1-i]){
n=n+1;
}
}
//Displaying if the entered word is palindrome or not
if (n==0) {
printf("It is a palindrome.");
}
else {
printf("It is not a palindrome.");
}
return 0;
}
Output
Enter a word to check palindrome: Reviver
It is a palindrome.
Comments