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.

Also Read: Strong Number in C Programming

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 pointer with numbers.

Also read : C Program to Find the Sum of Cubes of Elements in an Array

Expected Output

Enter any string
nayan
The string nayan is a palindrome.

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

Logic of the program to check palindrome in c using pointers.

This is a very important program in c to check palindrome using pointers. Here, we are using pointers, that means we are dealing with the addresses where the string is stored. We need two pointer variables. Why? One pointer variable will store the address of the first character and the other pointer variable will store the address of the last character.

Also Read: Floyd Triangle in C Programming

Next, we will compare first character with last character, second character with last second character and so on with the help of these two pointer variables. If we are able to compare whole string then that string is palindrome otherwise that string is not palindrome.

See the following program which is showing a string is palindrome or not using pointers in c programming.

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;
}

The above c program is checking whether a given a string is palindrome or not using pointers. Now, I am going to explain this program line by line.

Also read : Switch Case in C Program to Calculate Area of Circle and Triangle

Detailed Explanation

char str[30],*p1,*p2;

The above statement is variable declaration statement. In this declaration, we have declared a character array of 30 characters i.e. str[30] and two pointer variables i.e. *p1 and *p2.

In the next two statements, we are informing the user to enter a string and this string is read by gets() function. The gets() function is the unformatted input statement which is used to read the string. Let suppose, the user has entered the string “NAYAN“.

p1=str+strlen(str);

Before, explaining this statement, see the following diagram which shows the imaginary memory organization for all the variables.

memory organization for palindrome  program using pointers in c
Program to check palindrome in c using pointers

Now, it will be easy for you to understand the above statements. Here, str means the base address of the character array str[30]. From the above diagram, str means &str[0]. The &str[0] is 1000, &str[1] is 1001 and so on. Now, strlen() function is used to count the number of characters in string str. After executing the statement p1=str+strlen(str), the value of p1 becomes 1005 which is the location of null character i.e. ‘\0’.

p2=str;

From the above statement, the value p2 becomes 1000 which is base address or starting address of str.

p1–;

Here, we are decrementing the address of p1 from 1005 to 1004.

So, these above statements are declaration and initialization statements.

The while loop

while(p1>=p2)
    {
        if(*p1==*p2)
        {
            p1--;
            p2++;
        }
        else
        {
            break;
        }
    }

The above while loop will be executed until the value of p1 is greater than or equal to the value of p2. Here, the value of p1 is 1004 and p2 is 1000. Now, inside the while loop, there is if-else statement.

if(*p1==*p2)

Here, * is value at address operator. So, in the above if statement, we are comparing value at address of p1 and value at address of p2. In this case, value at address p1=1004 is ‘N’ and value at address p2=1000 is ‘N’. So, the condition in this if statement is true, therefore, we will compare next alphabet. For this, we have to decrement the value of p1 and increment the value of p2.

Now, the value of p1 becomes 1003 and that of p2 becomes 1001. Here, again both address have same alphabet and we will have to repeat the steps again and again until the value of p1 is greater than or equal to p2.

But, if the condition get false, then else part will be executed. In the else statement, we have written “break” statement which transfers the control out of the loop. Because this is clearly showing that characters are matched. Therefore, there is no need to compare further.

So, this was the while loop in c where we are checking palindrome using pointers. Now, in the next statements we are displaying whether the string is palindrome or not.

if(p2>p1)

In the above while loop, if the characters are matching till the end, the value of p2 will become greater than the value of p1. So, when this condition is true, then the string is palindrome. But, if any character does not match then the value p1 will remain greater than p2. Therefore, else part will be executed and we will get the message than the string is not a palindrome.

There are many ways to do this program to check palindrome in c using pointers. But, this is the simplest explanation I have tried. I hope you have understood this program. Thanks for reading.

Leave a Comment