Show the call stack for isPalindrome("abcba") using the functions defined in Listings and ,respectively.
Listings RecursivePalindrome.cpp
1#include
2#include
3using namespace std;
4
5bool isPalindrome(const string& s)
6 {
7 if (s.size() <= 1) // Base case
8 return true;
9 else if (s[0] != s[s.size() - 1]) // Base case
10 return false;
11 else
12 Return is Palindrome(s.substr(1, s.size() - 2));
13 }
14
15int main()
16 {
17 cout << "Enter a string: ";
18 String s;
19 getline(cin, s);
20
21 if (isPalindrome(s))
22 cout << s << " is a palindrome" << endl;
23 else
24 cout << s << " is not a palindrome" << endl;
25
26 return 0;
27 }
Listings RecursivePalindromeUsingHelperFunction.cpp
1#include
2#include
3using namespace std;
4
5bool isPalindrome(const string& s, int low, int high)
6 {
7if (high <= low) // Base case
8 return true;
9 else if (s[low] != s[high]) // Base case
10 return false;
11 else
12 Return is Palindrome(s, low + 1, high - 1);
13 }
4
15 bool isPalindrome(const string& s)
16 {
17 return isPalindrome(s, 0, s.size() - 1);
18 }
19
20 int main()
21 {
22 cout << "Enter a string: ";
23 string s;
24 getline(cin, s);
25
26if (isPalindrome(s))
27 cout << s <<"is a palindrome" << endl;
28 else
29 cout << s << " is not a palindrome" << endl;
30
31 return 0;
32 }
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.