String Palindrome in C
To find the wheather a string is palindrome or not first we want to know what is a palindrome.
String Palindrome:
A string palindrome is one in which on reversing a string the reversed string and the original string are same.
example : original string: abba
Reverse of String : abba
Therefore the string and its reverse order are same.
So first you need to find the reverse of string later you must verify wheater it is matching with original or not.
To understand more about string reverse you can refer this post.
int main(){char s[100],s1[100];int len,i,j,flag;scanf("%s",s);i=0;while(s[i]!='\0'){i++;}len=i;j=0;for(i=len-1;i>=0;i--){s1[j]=s[i];j++;}s1[j]='\0';//check wheather two strings are palindrome or notflag=0;i=0;while(s[i]!='\0'){if(s[i]!=s1[i]){flag=1;break;}i++;}if(flag==1)printf("Not a palindrome");elseprintf("It is a palindrome");return 0;}
input: level
output:It is a palindrome
No comments:
Post a comment