check weather a string is palindrome or not using pointers

check weather a string is palindrome or not using pointers

#include<stdio.h>
int main()
{
char str[100],*p,*r;
scanf("%s",str);
p=str;//assign address of str into p
while(*p!='\0')
{
    ++p;
}// the purpose of this loop is to reach the end of the string .now p points to last character i.e '\0'
--p;//decrement because to move to last character
r=str;// r is a pointer to store the result and it aso contains base address of string
while(p>=r) //in this p moves in from end to begin and r moves from start to end untill middle occurs
{
    if(*p==*r)//check at each stage
    {
        --p;
        ++r;
    }
    else
    break;
}
if(r>p)// if r crosses p means it is palindrome
printf("palindrome");
else
printf("not a palindrome");
return 0;
}

No comments:

Post a Comment