0
0
CProgramBeginner · 2 min read

C Program to Check Palindrome Using Recursion

You can check if a string is a palindrome in C using recursion by comparing the first and last characters with a function like int isPalindrome(char str[], int start, int end) that returns 1 if palindrome and 0 otherwise.
📋

Examples

Inputmadam
Outputmadam is a palindrome
Inputhello
Outputhello is not a palindrome
Inputa
Outputa is a palindrome
🧠

How to Think About It

To check if a string is a palindrome using recursion, compare the first and last characters. If they match, call the function again with the next inner substring by moving the start index forward and the end index backward. If the start index crosses the end, the string is a palindrome. If any characters don't match, it is not.
📐

Algorithm

1
Get the input string.
2
Create a recursive function that takes the string, start index, and end index.
3
If start index is greater than or equal to end index, return true (palindrome).
4
If characters at start and end indexes are not equal, return false (not palindrome).
5
Otherwise, call the function recursively with start+1 and end-1.
6
Print the result based on the recursive function's return value.
💻

Code

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);
    int len = strlen(str);
    if (isPalindrome(str, 0, len - 1)) {
        printf("%s is a palindrome\n", str);
    } else {
        printf("%s is not a palindrome\n", str);
    }
    return 0;
}
Output
Enter a string: madam madam is a palindrome
🔍

Dry Run

Let's trace the string 'madam' through the code

1

Initial call

isPalindrome('madam', 0, 4) compares 'm' and 'm' - equal, recurse

2

Second call

isPalindrome('madam', 1, 3) compares 'a' and 'a' - equal, recurse

3

Third call

isPalindrome('madam', 2, 2) start >= end, return 1 (true)

4

Return back

All previous calls return 1, final result is palindrome

CallStart IndexEnd IndexCharacters ComparedResult
104'm' vs 'm'Recurse
213'a' vs 'a'Recurse
322Single char, base caseReturn 1
💡

Why This Works

Step 1: Base Case

When start >= end, it means we checked all pairs or reached the middle, so the string is a palindrome.

Step 2: Character Comparison

If characters at start and end are different, the string is not a palindrome, so return 0 immediately.

Step 3: Recursive Call

If characters match, call the function again with start + 1 and end - 1 to check the next inner pair.

🔄

Alternative Approaches

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

int isPalindromeIter(char str[]) {
    int start = 0, end = strlen(str) - 1;
    while (start < end) {
        if (str[start] != str[end]) {
            return 0;
        }
        start++;
        end--;
    }
    return 1;
}

int main() {
    char str[100];
    printf("Enter a string: ");
    scanf("%99s", str);
    if (isPalindromeIter(str)) {
        printf("%s is a palindrome\n", str);
    } else {
        printf("%s is not a palindrome\n", str);
    }
    return 0;
}
Uses a loop instead of recursion; simpler stack usage but less elegant for learning recursion.
Using two pointers with pointers arithmetic
c
#include <stdio.h>
#include <string.h>

int isPalindromePtr(char *start, char *end) {
    if (start >= end) return 1;
    if (*start != *end) return 0;
    return isPalindromePtr(start + 1, end - 1);
}

int main() {
    char str[100];
    printf("Enter a string: ");
    scanf("%99s", str);
    if (isPalindromePtr(str, str + strlen(str) - 1)) {
        printf("%s is a palindrome\n", str);
    } else {
        printf("%s is not a palindrome\n", str);
    }
    return 0;
}
Uses pointer arithmetic for recursion, which is more idiomatic C but requires understanding pointers.

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

Time Complexity

The function compares pairs of characters from start and end moving inward, so it runs in linear time proportional to the string length.

Space Complexity

Each recursive call adds a stack frame, so space used is proportional to the string length (O(n)).

Which Approach is Fastest?

The iterative approach uses O(1) space and is faster in practice due to no recursion overhead, but recursion is clearer for learning.

ApproachTimeSpaceBest For
RecursiveO(n)O(n)Learning recursion, clear logic
IterativeO(n)O(1)Performance and memory efficiency
Pointer recursionO(n)O(n)Idiomatic C pointer use
💡
Always check the base case first in recursion to avoid infinite calls.
⚠️
Beginners often forget to move the start and end indexes inward in the recursive call, causing infinite recursion.