-
palindrome
i've been trying to make this function work... but to no avail :(
Code:
pal (int *str)
{
int i=0,k,flag;
char x[10];
while(*(str+i)!='\0')
{
i++;
}
printf("%d", i);
k=i-1;
i=0;
while( k>=0)
{
x[i]=*(str+k);
printf("%c", *(str+k));
k--; i++;
}
flag=0;
k=i;
i=0;
while(*(str+i)!= '\0')
{
if (*(str+i) != x[i])
{
flag=1;
break;
}
i++; k--;
}
if(flag==0)
{
printf("Palindrome");
}
if (flag==1)
{
printf("Not Palindrome");
}
}
Can anyone help me plz :confused:
-
Here's an easy way:
Code:
for( x = 0, y-1 = strlen( foo ); x < strlen( foo ); x++,y-- )
{
if( foo[x]!=foo[y] )
printf( "Not a palindrome." );
}
You could of course optimize this to use only half the length, but that's the easiest way. Of course, this doesn't count or allow for white space, and it doesn't check case sensitivity.
Quzah.
-
You have declared str as int *
This could be ok, but then you would have to cast it to char
before comparing with '\0'
like
while(((char)*(str+i))!= '\0')
easiest would be to change int *str to char *str