C program to check whether a string is palindrome or not

Write a C program to check whether a string is palindrome or not without using loop. How to check whether a string is palindromic string or not in C programming. Logic to check palindrome string in C program.

Example

Input

Input string: madam

Output

Palindrome string

Required knowledge

Basic C programming, For loop, If else, String

Must read –

What is Palindromic string?

Palindrome string is a special string which reads same from backward or forward such as madam, mom, eye, dad etc.

Read more – Program to check palindrome number

Logic to check palindrome string

The basic idea behind checking palindrome is if it can be read same from forward and backward then it is palindrome else not. Here in the below algorithm we will traverse the string character by character in both direction at the same time if they are equal then the string is palindrome.
Below is the step by step descriptive logic to check palindrome string.

  1. Input a string from user, store it in some variable say str.
  2. Find length of the given string and store it in some variable say endIndex.
  3. Initialize another variable, to traverse the string in forward direction say startIndex = 0.
  4. Run a loop until either startIndex >= endIndex or str[startIndex] != str[endIndex]. Otherwise increment startIndex and decrement endIndex.
  5. Finally after loop check if startIndex >= endIndex then string is palindrome.

Program to check palindrome string

/**
 * C program to check whether a string is palindrome or not
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
    char str[MAX_SIZE];
    int len, startIndex, endIndex;

    /* Input string from user */
    printf("Enter any string: ");
    gets(str);


    /* Find length of the string */
    len = 0;
    while(str[len] != '\0') len++;

    startIndex = 0;
    endIndex   = len-1;
    

    while(startIndex <= endIndex)
    {
        if(str[startIndex] != str[endIndex])
            break;

        startIndex++;
        endIndex--;
    }

    if(startIndex >= endIndex)
    {
        printf("String is Palindrome.");
    }
    else
    {
        printf("String is Not Palindrome.");
    }

    return 0;
}

You can also use inbuilt string library functions to make the task easier. Using inbuilt string library function you just need to find reverse of string, then compare it with original string.

Program to check palindrome string using string functions

/**
 * C program to check whether a string is palindrome or not using string functions
 */

#include <stdio.h>
#include <string.h>

#define MAX_SIZE 100 // Maximum string size

int main()
{
    char str[MAX_SIZE], reverse[MAX_SIZE];
    int flag;

    /* Input string from user */
    printf("Enter any string: ");
    gets(str);


    strcpy(reverse, str); //Copies original string to reverse
    strrev(reverse);      //Finds the reverse of string

    flag = strcmp(str, reverse); //Checks whether both are equal or not

    /* If both strings are equal */
    if(flag == 0)
    {
        printf("String is Palindrome.");
    }
    else
    {
        printf("String is Not Palindrome.");
    }

    return 0;
}

Output

Enter any string: madam
String is Palindrome.

Happy coding 😉