C Program
#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[] = "madam"; if(isPalindrome(str, 0, strlen(str) - 1)) printf("%s is a palindrome\n", str); else printf("%s is not a palindrome\n", str); return 0; }
C Output
Input: madam Output: madam is a palindrome
C++ Program
#include <iostream> #include <string> using namespace std; bool isPalindrome(string str, int start, int end) { if(start >= end) return true; if(str[start] != str[end]) return false; return isPalindrome(str, start + 1, end - 1); } int main() { string str = "racecar"; if(isPalindrome(str, 0, str.size() - 1)) cout << str << " is a palindrome\n"; else cout << str << " is not a palindrome\n"; return 0; }
C++ Output
Input: racecar Output: racecar is a palindrome
JAVA Program
public class Main { static boolean isPalindrome(String str, int start, int end) { if(start >= end) return true; if(str.charAt(start) != str.charAt(end)) return false; return isPalindrome(str, start + 1, end - 1); } public static void main(String[] args) { String str = "level"; if(isPalindrome(str, 0, str.length() - 1)) System.out.println(str + " is a palindrome"); else System.out.println(str + " is not a palindrome"); } }
JAVA Output
Input: level Output: level is a palindrome
Python Program
def is_palindrome(s, start, end): if start >= end: return True if s[start] != s[end]: return False return is_palindrome(s, start + 1, end - 1) word = "noon" if is_palindrome(word, 0, len(word) - 1): print(f"{word} is a palindrome") else: print(f"{word} is not a palindrome")
Python Output
Input: noon Output: noon is a palindrome
Explanation
Example
A palindrome is a phrase, word, or sequence that is the same when read forward and backward. For instance, "madam" is still "madam" when turned around. The recursive approach compares the first and last characters, and if they're the same, it calls itself for the substring excluding them. This goes on until either a mismatch or reaching the middle of the string, which indicates it's a palindrome.
Real-Life Analogy
Suppose you have a folded piece of paper with letters on both sides. You unfold it from both sides and verify whether the outermost letters are same. If they are, you fold inside and verify the next pair. If ever the letters are not the same, you know immediately it's not a palindrome. Recursion for this problem is similar to that folding action.
Why It Matters
Palindrome checking is not an exercise in the book. It develops recursive thought processes, base case handling, and dividing a problem into smaller subproblems. In real-world applications, palindrome reasoning surfaces in DNA sequence examination, data verification, and some cryptosystems where symmetry is important.
Complexity and Optimization
Time complexity is O(n) with n being the string length, since each comparison advances one step from both sides. Space complexity is O(n) because of the recursion call stack. Space can be minimized to O(1) using iterative methods, but the recursive method is neater and more intuitive to learn.
Edge Cases and Testing
Trivially palindromes are single-character strings or empty strings. Results may be influenced by case sensitivity, so it is better to convert everything to lowercase before checking. Spaces and punctuation must be stripped if the sentence palindromes such as "A man, a plan, a canal: Panama" are being checked.
Interview Tips
In interviews, palindrome checking is commonly employed to assess recursion understanding, string manipulation, and edge case handling. An accompanying follow-up would be to expand the function to deal with phrases, numbers, or to skip special characters, which tests flexibility.
SEO-Friendly Closing
Palindrome check using recursion in C, C++, Java, and Python is an easy yet effective programming task that solidifies recursive thinking, imparts handling base cases, and provides immediate uses in text processing and algorithm design. Solving this problem helps beginners familiarize themselves with thinking recursively, effectively handling function calls, and acclimatize themselves to coding interviews where string problems are routine. This strategy is also useful for practical applications like data pattern matching, genetic sequence analysis, and natural language processing where symmetrical patterns are important.
Social Plugin