Check Palindrome String Using Recursion
C++
Hard
5 views
Problem Description
Check if string reads same forwards and backwards using recursion.
Real Life: Words like "madam", "radar" that read same both ways.
Step-by-Step Logic:
1. Base case: if string empty or single char, it's palindrome
2. Check if first and last characters match
3. If yes, recursively check remaining middle string
4. If no, not a palindrome
Official Solution
bool isPalindromeRecursive(string str, int start, int end) {
if(start >= end) {
return true; // Base case
}
if(str[start] != str[end]) {
return false;
}
return isPalindromeRecursive(str, start + 1, end - 1);
}
void function_q13_palindrome() {
string word = "radar";
bool result = isPalindromeRecursive(word, 0, word.length() - 1);
if(result) {
cout << word << " is a palindrome" << endl;
} else {
cout << word << " is NOT a palindrome" << endl;
}
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!