C Program to Check whether the given String is Palindrome

In this blog post, you will learn how to write a C Program to Check whether the given String is Palindrome. But before writing the C code to check whether a given string is a palindrome first let’s understand what a palindrome string is.

A string is called a palindrome string if the reverse of the given string is the same as the original string. For example, “ABCDCBA” is a palindrome because the reverse of “ABCDCBA” will be equal to “ABCDCBA” so both of these strings are equal and are said to be a palindrome, but “ABCDBA” is not a palindrome.

Now I believe you are able to understand what palindrome string is. So let’s see the problem statement.

Problem statement:- “ Given a  string str, check whether the str is palindrome or not using the C programming language”

Constraints:

1 <= length of string <= 2 * 10^5

 

Approach-1:

In this approach, you have to follow the below-mentioned steps:

  1. Make two indexes l (low) and h (high) respectively.
  2. Initialize a flag variable as 0.
  3. now while (h>l), we will check if str[l] is equal to str[h].
  4. If str[l] is not equal to str[h],  make the flag 1 and break the loop otherwise in each iteration, increment the low index by 1 and decrease the high index by 1.
  5. After the loop execution, check whether the flag value is 0 or 1, If it’s 0 that means that the string is palindrome otherwise not a palindrome number”.
/*

C Program to Check whether the given String is Palindrome
*/
#include <stdio.h>
#include <string.h>


int main()
{
    // initializing string
    char str[] = "ABCDCBA";
    
    int l = 0; //string first char index
    int h = strlen(str)-1; //last char index
    int flag = 0;

    // Checking conditions in the loop
    while (l<=h)
    {
        //  If string are unequal return No
        if (str[l++] != str[h--])
        {
            flag = 1;
            break;
        }
    }

    if(flag != 0)
    {
        printf("Not a Palindrome String \n");
    }
    else
    {
        printf("Palindrome String \n");
    }

    return 0;
}

 

Approach 2 – Using a Function:

The approach is the same as the above code but inside the writing of all code in the main function; I will create a separate C function to check whether a given string is a palindrome string or not.

/*

C Program to Check whether the given String is Palindrome
*/
#include <stdio.h>
#include <string.h>


int isPalindrome(const char *pStr)
{
    int l = 0; //string first char index
    int h = strlen(pStr)-1; //last char index
    int flag = 0;

    // Checking conditions in the loop
    while (l<=h)
    {
        //  If string are unequal return No
        if (pStr[l++] != pStr[h--])
        {
            flag = 1;
            break;
        }
    }

    return flag;
}


int main()
{
    char str[] = "ABCDCBA";
    //function calling to check Palindrome String
    const int isPalindromeString = isPalindrome(str);
    if(isPalindromeString)
    {
        printf("Not a Palindrome String \n");
    }
    else
    {
        printf("Palindrome String \n");
    }

    return 0;
}

 

 

Approach 3 – Using Recursion:

We can also check if a string is a palindrome string or not. If you don’t know about the recursive function, you can check my blog post  “Recursion in C“.

Now let’s see the steps to find a palindrome string using the recursion.

  1. Pass the index of the first character and last character of the string along with the string.
  2. Initially, the index of the first character would be 0 and the last character n-1 where n is the length of the string to be checked.
  3. In the recursive function, we need to check whether the characters at the index pointed by l and h are equal or not.
  4. If in the recursive calls, characters str[l] and str[h] are equal, pass the string by increasing the l index value by 1 and decreasing the h index value by 1.
/*

C Program to Check whether the given String is Palindrome
*/
#include <stdio.h>
#include <string.h>


int isPalindrome(const char *pStr, int l, int h)
{
    if(NULL == pStr || l < 0 || h < 0)
    {
        return 1;
    }
    //We reached at the mid of string
    if(l == h)
    {
        return 0;
    }
    //If unequal return 1
    if (pStr[l++] != pStr[h--])
    {
        return 1;
    }

    if( l <= h)
    {
        return isPalindrome(pStr, (l+1), (h-1));
    }

    return 0;
}


int main()
{
    char str[] = "ABCDCBA";
    int l = 0; //string first char index
    int h = strlen(str)-1; //last char index

    //function calling to check Palindrome String
    const int isPalindromeString = isPalindrome(str,l, h);
    if(isPalindromeString)
    {
        printf("Not a Palindrome String \n");
    }
    else
    {
        printf("Palindrome String \n");
    }

    return 0;
}

 

Bonus Problem:

Problem Statement: Write a function to check whether a given phrase is a palindrome or not. You can ignore uppercase, lowercase,  white spaces, and other characters to consider sentences as palindrome. Just consider only alphanumeric characters (a combination of alphabetical and numerical characters).

Let’s see a few examples to understand the questions.

Example 1:

Input: str = “Aticleword  =:= rowelcitA ”
Output: palindrome string
Explanation: “AticlewordrowelcitA ” is a palindrome.

Example 2:

Input: str = “Aticleworld is a free platform”
Output: Not a palindrome string

Example 3:

Input: str = ” ”
Output: palindrome
Explanation: str is an empty string “” after removing non-alphanumeric characters.

Note: Don’t jump directly to the solution, try it out yourself first.

 

 

/*

C Program to Check whether the given String is Palindrome
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>


int isPalindrome(const char *pStr)
{
    int l = 0; //string first char index
    int h = strlen(pStr)-1; //last char index
    int isPalindromeString = 0;
    while(l< h)
    {
        //skip if not alphanumeric
        if(!isalnum(pStr[l]))
        {
            l++;
        } //skip if not alphanumeric
        else if(!isalnum(pStr[h]))
        {
            h--;
        } //if both are not equal
        else if(tolower(pStr[l])!=tolower(pStr[h]))
        {
            isPalindromeString = 1;
            break;
        }
        else
        {
            l++;
            h--;
        }
    }
    return isPalindromeString;

}


int main()
{
    char str[] = "Aticleword  =:= rowelcitA ";

    //function calling to check Palindrome String
    const int isPalindromeString = isPalindrome(str);
    if(isPalindromeString)
    {
        printf("Not a Palindrome String \n");
    }
    else
    {
        printf("Palindrome String \n");
    }

    return 0;
}

 

 

Recommended Posts for you:

Leave a Reply

Your email address will not be published. Required fields are marked *