Check The Programming Section
A string is plaindrome or not can be implemented various ways, as follows:
using library functions from string.h header file
by comparing first character with last character and so on till the half of that string
Using library functions
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 100
int main(){
char info[SIZE], rev[SIZE];
printf("Enter any string::");
fgets(info,SIZE,stdin);
info[strlen(info)-1]='\0';
strcpy(rev,info);
if(stricmp(strrev(info),rev)==0)
printf("%s is a palindrom string",info);
else
printf("%s is not a palindrom string",info);
}
In the above example, fgets is used to take input. fgets also append a new line character at the end of the string. Before reverse the string we need to adjust the string terminating character '\0'. Thats why, the source line info[strlen(info)-1]='\0'; is used to set the null character to the second last index of the string.
Thereafter info is copied to rev array and compared with the revese string of info insdie the if. For comparining stricmp is prefered over strcmp because stricmp compared alphabatically without regard to case.
By comparing each character from front with end
#include<stdio.h>
#include<stdlib.h>
#define SIZE 100
int getLength(char info[]){
int i = 0;
while(info[i]!='\0')
i++;
return i;
}
int checkPalindrome(char info[],int length){
int i,j;
for(i=0,j=length-1;i<j;i++,j--){
if(info[i]==info[j] || info[i]==info[j]+32 || info[i]==info[j]-32)
continue;
else break;
}
if(i==j) return 1;
else return 0;
}
int main(){
char info[SIZE];
printf("Enter any string::");
fgets(info,SIZE,stdin);
int length = getLength(info);
info[--length]='\0';
int i = checkPalindrome(info,length);
if(i==1) printf("%s is a palindrome string",info);
else printf("%s is a palindrome string",info);
}