0
0
CProgramBeginner · 2 min read

C Program to Check Palindrome String

A C program to check palindrome string reads the input string, compares characters from start and end using a loop, and prints "Palindrome" if all match or "Not Palindrome" otherwise.
📋

Examples

Inputmadam
OutputPalindrome
Inputhello
OutputNot Palindrome
Inputa
OutputPalindrome
🧠

How to Think About It

To check if a string is a palindrome, compare the first character with the last, the second with the second last, and so on. If all pairs match, the string reads the same forward and backward, so it is a palindrome. If any pair differs, it is not.
📐

Algorithm

1
Get the input string from the user.
2
Set two pointers: one at the start and one at the end of the string.
3
Compare characters at these pointers.
4
If characters differ, stop and conclude it is not a palindrome.
5
Move pointers closer to the center and repeat until they meet or cross.
6
If all pairs matched, conclude the string is a palindrome.
💻

Code

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

int main() {
    char str[100];
    int i, j, isPalindrome = 1;

    printf("Enter a string: ");
    scanf("%99s", str);

    i = 0;
    j = strlen(str) - 1;

    while (i < j) {
        if (str[i] != str[j]) {
            isPalindrome = 0;
            break;
        }
        i++;
        j--;
    }

    if (isPalindrome)
        printf("Palindrome\n");
    else
        printf("Not Palindrome\n");

    return 0;
}
Output
Enter a string: madam Palindrome
🔍

Dry Run

Let's trace the input "madam" through the code.

1

Input string

str = "madam"

2

Initialize pointers

i = 0 (points to 'm'), j = 4 (points to 'm')

3

Compare characters

str[i] == str[j] ('m' == 'm'), move i to 1, j to 3

4

Compare characters

str[i] == str[j] ('a' == 'a'), move i to 2, j to 2

5

Pointers meet

i == j (both 2), middle character 'd', no mismatch found

6

Result

All pairs matched, string is palindrome

ijstr[i]str[j]Match?
04mmYes
13aaYes
22ddYes
💡

Why This Works

Step 1: Two-pointer comparison

Using two pointers, one at the start and one at the end, lets us compare characters from both ends moving inward.

Step 2: Early exit on mismatch

If any pair of characters differ, we can stop immediately because the string cannot be a palindrome.

Step 3: Complete check

If all pairs match until pointers meet or cross, the string reads the same forwards and backwards, confirming it is a palindrome.

🔄

Alternative Approaches

Reverse string and compare
c
#include <stdio.h>
#include <string.h>

int main() {
    char str[100], rev[100];
    int len, i;

    printf("Enter a string: ");
    scanf("%99s", str);

    len = strlen(str);
    for (i = 0; i < len; i++) {
        rev[i] = str[len - i - 1];
    }
    rev[len] = '\0';

    if (strcmp(str, rev) == 0)
        printf("Palindrome\n");
    else
        printf("Not Palindrome\n");

    return 0;
}
This method uses extra space for the reversed string but is easy to understand.
Recursive check
c
#include <stdio.h>
#include <string.h>

int isPalindrome(char str[], int start, int end) {
    if (start >= end) return 1;
    if (str[start] != str[end]) return 0;
    return isPalindrome(str, start + 1, end - 1);
}

int main() {
    char str[100];
    printf("Enter a string: ");
    scanf("%99s", str);

    if (isPalindrome(str, 0, strlen(str) - 1))
        printf("Palindrome\n");
    else
        printf("Not Palindrome\n");

    return 0;
}
This recursive method is elegant but uses call stack space proportional to string length.

Complexity: O(n) time, O(1) space

Time Complexity

The program compares characters from both ends once, so it runs in linear time proportional to the string length.

Space Complexity

It uses constant extra space by comparing in place without storing additional copies.

Which Approach is Fastest?

The two-pointer method is fastest and most memory efficient compared to reversing the string or recursion.

ApproachTimeSpaceBest For
Two-pointer comparisonO(n)O(1)Fastest and memory efficient
Reverse string and compareO(n)O(n)Simple but uses extra memory
Recursive checkO(n)O(n)Elegant but uses call stack
💡
Always check characters from both ends moving inward to efficiently detect palindrome.
⚠️
Beginners often forget to stop comparing when pointers meet or cross, causing unnecessary checks.