Home » C Program » C Program to check given String is palindrome or not

C Program to check given String is palindrome or not

Last updated:

Support this post with a reaction:

2,581 views

A palindrome string is a string that reads the same from left to right and right to left. In C, we can check whether a string is a palindrome by comparing its first and last characters, then moving towards the middle.

For example, madam, level, radar, and malayalam are palindrome strings because their characters remain the same when reversed.

In this tutorial, you will learn how to write a C program to check whether a given string is a palindrome or not using a loop. You will also understand the algorithm, program execution, dry run, output, and time complexity.

What Is a Palindrome String in C?

A palindrome string is a sequence of characters that remains the same when read in both forward and reverse directions.

Examples of palindrome strings:

  • madam
  • level
  • radar
  • refer
  • malayalam

Examples of non-palindrome strings:

  • hello
  • computer
  • quescol
  • program

For example:

Palindrome: MADAM

Original String:  M A D A M
                  ↓       ↓
                  M = M
                    ↓   ↓
                    A = A
                      D

Not a Palindrome: HELLO

Original String:  H E L L O
                  ↓       ↓
                  H != O

Therefore, MADAM is a palindrome, whereas HELLO is not.

C Program to Check Whether a String Is Palindrome or Not

The following C program checks whether the entered string is a palindrome by comparing characters from both ends.

Program:

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

int main() {
    char str[100];
    int start, end;
    int isPalindrome = 1;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove newline character
    str[strcspn(str, "\n")] = '\0';

    start = 0;
    end = strlen(str) - 1;

    // Compare characters from both ends
    while (start < end) {
        if (str[start] != str[end]) {
            isPalindrome = 0;
            break;
        }

        start++;
        end--;
    }

    if (isPalindrome) {
        printf("The string is a palindrome.");
    } else {
        printf("The string is not a palindrome.");
    }

    return 0;
}

Output 1: When the String Is a Palindrome

Enter a string: madam
The string is a palindrome.

Output 2: When the String Is Not a Palindrome

Enter a string: quescol
The string is not a palindrome.

Output 3: When the String Contains Spaces

Enter a string: madam im adam
The string is not a palindrome.

Note: This program performs a case-sensitive comparison and considers spaces and special characters. Therefore, Madam and madam are treated differently.

How Does a Palindrome String Program Work?

The palindrome checking logic works by comparing characters from opposite ends of a string.

  1. Read a string from the user.
  2. Calculate the length of the string.
  3. Initialize two positions: start = 0 and end = length - 1.
  4. Compare the characters at the start and end positions.
  5. If both characters are equal, move the start position forward and the end position backward.
  6. Repeat the process until the positions meet or cross.
  7. If all corresponding characters match, the string is a palindrome. Otherwise, it is not.