Friday, July 31, 2009

C-prog 2 chk if a given string is a palindrome using strrev?

actually the logic which i used was


char *s,*t ;


int x ;


printf("enter the string");


gets(s);


t=strrev(s);


x=strcmp(s,t);


if(x==0)


printf("palindrome");


else


printf("not palindrome");


getch();





the problem is that what ever string i enter the ouput says tht its a palindrome!!


friends ... plz help me out !!!???

C-prog 2 chk if a given string is a palindrome using strrev?
#include%26lt;stdio.h%26gt;


#include%26lt;string.h%26gt;





void main()


{


char s[20],t[20] ;


int x ;


printf("Enter the string : ");


gets(s);


strcpy(t,s);


strrev(s);


x=strcmp(s,t);


if(x==0)


printf("\nPalindrome");


else


printf("\nNot palindrome");


}





Actually strrev() reverses the original string,so in your program s %26amp; t were getting the same value irrespective of whatever you entered.Here I've copied the original value to t before reversing s.That solves your problem :)


also I would advise you not to use the gets() function as it is dangerous to use ,it may create problem in the memory structure,use scanf instead.
Reply:One major problem is in your program is you are using single character for s, and t; To declare string use





char s[10], t[10];


and modify your program accordingly














use strcmp() as follows


x=strcmp(*s, *t);
Reply:you r having x as integer and strcmp return %26lt;1or%26gt;1 if strings are not same so int converts a no like 0.something to zero,and string is always palindrome.just use statement


if(strcmp(s,t)) instead of putting value in x,it also decrease a variable in your program.
Reply:declare the char *s,*t as array


int s[10],t[10]


there u will get the perfect result.


No comments:

Post a Comment