Hi can you guys please help me with this question, and keep it in entry level c+
ID: 3860204 • Letter: H
Question
Hi can you guys please help me with this question, and keep it in entry level c++ programming ... thank you! Create a program that includes a function called reverse that takes a string and returns a string in the reverse order. For example, given “hello” as the input, the function should return “olleh”, which is the reversed string of “hello”. The main function should keep prompt the user to input a string until the user types “Q” For each string input call the function with the string and display the result. Note that the user input string may contain white spaces. Test your program with the following input. a) “Hello” (your program should display “olleH”) b) “CBU” (your program should display “UBC”) c) “C++ is fun!” (your program should display “!nuf si ++C”) d) “Q” (should exit the program)
Explanation / Answer
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
string reverse(string s){
string reverseString = "";
for(int i=s.length()-1; i>=0; i--){
reverseString = reverseString + s[i];
}
return reverseString;
}
// start main function
int main()
{
string s = "";
while(s != "Q"){
cout<<"Enter the string: ";
getline(cin , s);
if(s!="Q"){
cout<<"Reverse string is "<<reverse(s)<<endl;
}
}
return 0;
}
Output:
sh-4.2$ g++ -std=c++11 -o main *.cpp
sh-4.2$ main
Enter the string: hello
Reverse string is olleh
Enter the string: CBU
Reverse string is UBC
Enter the string: Hello
Reverse string is olleH
Enter the string: Q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.