The goal of this program is to get user input and then print the words backwards, but still in the order the user typed them in. For example, the user input- "cats and mice are cool", then the program should output "stac dna ecim era looc", but what I am getting is "looc era ecim dna stac". I think that rearranging the words would happen in the main function, but I'm not sure how to get it to print in order. Any help greatly appreciated!
#include <iostream>
#include <cstring>
using namespace std;
void reverse(string input) {
int size = (int)input.size();
if(size==1){
cout << input[size-1];
}
else {
cout << input[size-1];
reverse(input.substr(0, size-1));
}
}
int main() {
string input;
char choice;
cout << "Please enter a string with spaces." << endl;
getline(cin, input);
reverse(input);
cout << endl;
}