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
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);
}
Comments
Dear Mark, please add header to the first line:
# include <iostream>
The program doesn't run like the problem was in the reverse string I don't know how to fix?
Leave a comment