Answer to Question #305920 in C++ for Juliette

Question #305920

STRING REVERSE PROGRAM

Create a program that will apply recursive functions.

The program should accept a string say SUBJECT.

The output of the program should be TCEJBUS .

Screen/Layout

Input a string: SUBJECT

After a reverse: TCEJBUS


Try Another[Y/N]: Y


Input a string: face

After reverse: ecaf


Try Another[Y/N]:N



1
Expert's answer
2022-03-04T02:15:51-0500
using namespace std;


/*
STRING REVERSE PROGRAM
Create a program that will apply recursive functions.
The program should accept a string say SUBJECT.
The output of the program should be TCEJBUS .
Screen/Layout
Input a string: SUBJECT
After a reverse: TCEJBUS
Try Another[Y/N]: Y
Input a string: face
After reverse: ecaf
Try Another[Y/N]:N
*/


void ReverseString(string &u, int k)
{
    static int i = 0;
    if (k == u.length())	return;
    ReverseString(u, k + 1);
    if (i <= k)	swap(u[i++], u[k]);
}


int main()
{
	string s;
	char c = 'y';
	int Flag=1;
	
	while(Flag)
	{
		cout<<"\n\nInput a string: "; cin>>s;
		ReverseString(s,0);
		cout<<"\nAfter Reverse: "<<s;
		cout<<"\n\nTry another (Y/N): "; cin>>c;
		if(c=='y'||c=='Y') Flag=1; else Flag=0;
	}
	return(0);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
05.03.22, 20:11

Dear Mark, please add header to the first line:

# include <iostream>


Mark
05.03.22, 06:11

The program doesn't run like the problem was in the reverse string I don't know how to fix?

Leave a comment

LATEST TUTORIALS
New on Blog