Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Simple C++ program Important: You must use the array for this lab, no vectors al

ID: 3917187 • Letter: S

Question

Simple C++ program

Important: You must use the array for this lab, no vectors allowed.

Create a Recursive Function

Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character.

Example: Input of “Hello, world” should output “dlrow ,olleH(newline)”.

Note: Your recursive function just needs to print the reverse of the string rather than actually reversing the string itself.

Menu

Your program needs to demonstrate the functions by providing a menu.

The menu should provide user choices to select the function to call, after prompting user input for function call and the function outputs results, the menu should go back to the first menu to let the user continue choosing the function to call.

The menu should prompt the user to enter a string and your program reversely prints the string

Note: Must use getline() in standard library so the input takes space characters.

The menu must also provide the option to quit the program.

Explanation / Answer

#include <iostream>

using namespace std;
void reverse(string s) {
if(s.length() ==0) {
cout<<endl;
return;
}else {
cout<<s[s.length()-1];
reverse(s.substr(0,s.length()-1));
}
}
int main()
{
string s;
char choice = 'y';
while(!(choice == 'q' || choice == 'Q')) {
cout << "Enter the string:" << endl;
getline(cin,s);
cout<<"Choose: f. Function Call q. Quit"<<endl;
cin >> choice;
cin.ignore(100,' ');
  
if(choice =='f'){
cout<<"Reverse String: ";
reverse(s);
}
}
return 0;
}

Output:

q

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote