Wednesday, March 2, 2011

Character Arrays and Strings

Write a program that reads a string from the keyboard and determines whether the string is a palindrome or not. ( A string is palindrome if it can be read from left and right with the same meaning. For example, Madam and Anna are palindrome strings. Ignore capitalization).

#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main()
{
int i=0,st,x;
char c,line[20],line2[20];
clrscr();
printf("Enter the string or Name:");
while(c!='\n')
{
c=getchar();
if(islower(c))
c=toupper(c);
line[i]=c;
i++;
}
i=i-1;
line[i]='\0';
st=strlen(line);
for(i=0;i<=st-1;i++)
{
line2[i]=line[st-1-i];
}
line2[i]='\0';
x=strcmp(line,line2);
if(x==0)
printf("This is palindrome!");
else
printf("This is not palindrome!");
getch();
}

OUTPUT
Enter the string or Name : Suresh
This is not palindrome!

Enter the string or Name : Madam
This is palindrome!

No comments:

Post a Comment