C Program to Check Palindrome Using Recursion
int isPalindrome(char str[], int start, int end) that returns 1 if palindrome and 0 otherwise.Examples
How to Think About It
Algorithm
Code
#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; }
Dry Run
Let's trace the string 'madam' through the code
Initial call
isPalindrome('madam', 0, 4) compares 'm' and 'm' - equal, recurse
Second call
isPalindrome('madam', 1, 3) compares 'a' and 'a' - equal, recurse
Third call
isPalindrome('madam', 2, 2) start >= end, return 1 (true)
Return back
All previous calls return 1, final result is palindrome
| Call | Start Index | End Index | Characters Compared | Result |
|---|---|---|---|---|
| 1 | 0 | 4 | 'm' vs 'm' | Recurse |
| 2 | 1 | 3 | 'a' vs 'a' | Recurse |
| 3 | 2 | 2 | Single char, base case | Return 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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Recursive | O(n) | O(n) | Learning recursion, clear logic |
| Iterative | O(n) | O(1) | Performance and memory efficiency |
| Pointer recursion | O(n) | O(n) | Idiomatic C pointer use |