C++ Program to Check Palindrome String Using Recursion with Explanation
C++
Hard
Functions
40 views
1 min read
184 words
This problem helps you practice core C++ fundamentals in a practical way. It builds intuition around palindrome, recursion, check. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Check if string reads same forwards and backwards using recursion.
Real Life: Words like "madam", "radar" that read same both ways.
Input Format
A string S.
Output Format
Print
• Palindrome
OR
• Not a Palindrome
Constraints
• String length ≥ 1
• Case-sensitive (as given)
• Use recursion only
Concept Explanation
A string is a palindrome if it reads the same from left to right and right to left.
Recursion compares characters from both ends moving toward the center.
Step-by-Step Logic
1.Define a recursive function with parameters: string, left index, right index.
2.Base case:
• If left >= right, all characters matched → Palindrome.
3.Check step:
• If S[left] != S[right], return Not a Palindrome.
4.Recursive step:
• Call the function with left + 1 and right - 1.
5.Final result comes from recursive checks.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
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;
}
}
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Check if string reads same forwards and backwards using recursion.
Real Life: Words like "madam", "radar" that read same both ways.
Input / Output
Output
Print
• Palindrome
OR
• Not a Palindrome
Constraints
• String length ≥ 1
• Case-sensitive (as given)
• Use recursion only
Explanation
Concept Explanation
A string is a palindrome if it reads the same from left to right and right to left.
Recursion compares characters from both ends moving toward the center.
Step-by-Step Explanation
1.Define a recursive function with parameters: string, left index, right index.
2.Base case:
• If left >= right, all characters matched → Palindrome.
3.Check step:
• If S[left] != S[right], return Not a Palindrome.
4.Recursive step:
• Call the function with left + 1 and right - 1.
5.Final result comes from recursive checks.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
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!