Hey guys I am new at c++ I was just wondering if someone could show me how to print a string in reverse order(for example: "today is Wednesday " to "Wednesday is today"). My professor said we should begin with a null string and build it one word at a time. i have been working on this for weeks, and cannot figure it out. If someone can help me out,I would greatly appreciate it!!!
Thank You,
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
1.) Create some sort of std::string container (maybe a vector).
2.) Enter a loop, which separates words by finding white-space characters, and then pushes each word to the container.
2.5.) Ensure that any potential punctuation characters aren't considered to be part of a word
3.) Either:
a.) Reverse the elements of the container, and then print the elements in an ascending for loop.
b.) Print the elements of the container in a descending for loop.
But I suppose you haven't learned about vectors yet.
And, I suppose you have to, as your instructor said, "begin with a null string".
I'll try to post an example here soon.
It's kind of funky, because it adds each word with the letters in reverse order, but in the right word order, to the nullstring. Then, before we print, we reverse the elements of the nullstring, effectively re-reversing the order of the characters, but this time reversing the order of the words.
It also doesn't handle punctuation, and it only works if the sentence string ends with a space. I didn't think this was too much of a problem, because in your original example, the original string also ends with a space. However, this introduces an extrenuous space at the beginning of the nullstring.
Also, it doesn't handle capitalized letters in any way. All that stuff is for you to figure out.