No helper functions or Auxiliary functions. You must use recursion. Use magic fu
ID: 3856387 • Letter: N
Question
No helper functions or Auxiliary functions. You must use recursion. Use magic functions. Your code must be such that if we insert it into a suitable test framework with a main routine and appropriate #include directives, it compiles. Please show the base case and the recursive case. Here is a function, with descriptions of what they are supposed to do. They are incorrectly implemented. Replace the incorrect implementations of these functions with correct ones that use recursion in a useful way: your solution must not use the keywords while, for, or goto. You must not use global variables or variables declared with the keyword static, and you must not modify the function parameter lists. You must not use any references or pointers as parameters except for the parameters representing arrays. //str contains a single pair of parenthesis, return a new string //made of only the parenthesis and whatever those parensthesis //contain.////Pseudocode Example: subParen("abc(ghi)789'') = > "(gh) //subParen("(x)7") = > "(x)" //subParen("4agh(y)'') = > "(y)'.//string subParen(string str) { return"*";//This is incorrect. }Explanation / Answer
C++ Code:
#include <iostream>
#include <string>
using namespace std;
string subParen(string str)
{
if (str[0] == '(' && str[str.length()-1] == ')')
{
return str;
}
else if (str[0] != '(')
{
return subParen(str.substr(1));
}
else if (str[str.length()-1] != ')')
{
return subParen(str.substr(0, str.length()-1));
}
}
int main()
{
cout << subParen("abc(ghj)789") << endl;
cout << subParen("(xx)7") << endl;
cout << subParen("4agh(y)") << endl;
return 0;
}
---------------
OUTPUT:
$ g++ parenthesis.cpp
$ ./a.out
(ghj)
(xx)
(y)
NOTE: According to the question, you have to write only the function part. But I have given you the whole program so that you can see it working. If you need more help, you can ask in the comments.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.