1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <iostream>
using namespace std;
int main()
{
string str = "Hello World";
const static string display = "\nReversing string: ",
prompt = "\nEnter the string to reverse: ",
repeat = "\nWould you like to try again ('yes' or 'no'): ",
error = "\nInvalid entry, try again ('yes' or 'no'): ",
yes = "yes", no = "no";
cout<<"String Reversing Program\nDefault string: 'Hello World'\n";
do
{
cout<<display<<'\n';
cout<<": "<<str<<'\n'<<": ";
for(int i=0;i<str.size();++i) //Print manipulate: reverse string
cout<<str[str.size()-1-i];
cout<<repeat;
while(getline(cin,str)&&(str!=yes&&str!=no)) //Validate user choice repeat if(!"yes" or !"no")
cout<<error;
}while(str!=no && cout<<prompt && getline(cin,str)); //Repeat loop if not "no": prompt user if "no";
cout<<"\nPress <enter> to exit console: ";
cin.get(); //Console exit prompt
return 0;
}
|
String Reversing Program
Default string: 'Hello World'
Reversing string:
: Hello World
: dlroW olleH
Would you like to try again ('yes' or 'no'): incorrect
Invalid entry, try again ('yes' or 'no'): yes
Enter the string to reverse: It worked!
Reversing string:
: It worked!
: !dekrow tI
Would you like to try again ('yes' or 'no'): no
Press <enter> to exit console: |