Palindrome in C using Pointers

Palindrome in c using pointers

What is Palindrome?

In this article, I am going to write a program to check Palindrome in C using Pointers. But, before writing this program, we must know what is the meaning of palindrome? This can be used for both, string or a number. See the following examples.

The number 121 is a palindrome. How? If we read 121 from left to right or right to left, it remains 121. Is 233 a palindrome? A big NO. Why? When we read 233 from right to left, it becomes 332 which is not the same as 233. Similarly, we can also apply this to the string. The string “Nayan” is a palindrome. Read this name from both the end, it remains Nayan.

So, we can define palindrome as it is the number or string which remain the same when we read it from both the end.

Here, we are using only string palindrome. Because we need to write a program to check palindrome using pointers in c programming. It will be very difficult to use the pointer with numbers.

Expected Output

Enter any string
nayan
The string nayan is a palindrome.

Enter any string
vijay
The string vijay is not a palindrome.

Program to check Palindrome in C using Pointers

#include <stdio.h>
#include <string.h>
int main()
{
char str[30],*p1,*p2;
printf(“Enter any string\n”);
gets(str);
p1=str+strlen(str);
p2=str;
p1 — ;
while(p1>=p2)
{
if(*p1==*p2)
{
p1 — ;
p2++;
}
else
{
break;
}
}
if(p2>p1)
{
printf(“The string %s is a Palindrome”,str);
}
else
{
printf(“The string %s is not a Palindrome”,str);
}
return 0;
}

For detailed explanation, click here.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store