Printing string in reverse order

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;

int nwords(string);

int main()
{
string next;
int count;

ifstream mohsin("line.txt");

getline(mohsin,next);
if(mohsin)
{
count=nwords(next);
cout<<next<<endl;
getline(mohsin,next);
}
}

int nwords(string str)
{
int nextblank;
int nw=0;

nextblank=str.find(" ",0);

while(nextblank>0)
{
nw++;
str=str.substr(nextblank+1);
nextblank=str.find(" ",0);
}

return nw;
}

string reverse(string str){}
Here's an easy way of doing it:

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.
Last edited on
We are not allowed to use vectors for the class, it is only a introductory course.
Here's a weird example.

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
26
27
28
29
30
#include <iostream>
#include <string>

int main(int argc, char* argv[]) {

	std::string sentence = "This is a sentence ";

	std::string nullstring = "";
	std::string temp = "";

	for (unsigned short i = 0; i < sentence.size(); ++i) {
		if (sentence[i] != ' ') {
			temp += sentence[i];
		}
		else {
			temp = std::string(temp.rbegin(), temp.rend());
			nullstring += temp;
			nullstring += ' ';
			temp.clear();
		}
	}

	nullstring = std::string(nullstring.rbegin(), nullstring.rend());

	std::cout << sentence << std::endl;
	std::cout << nullstring << std::endl;

	std::cin.get();
	return 0;
}


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.
Last edited on
My program is pretty basic, but the sentence is read in from a file. would this be the same type of coding used if it were read in from a file.
Pretty much, yeah. This would entail reading from a file, properly parsing the contents, and then feeding that into a string possibly.
Topic archived. No new replies allowed.