C++ String Reverse Question

I was given a program to think about:
To read in a 4 word sentence from the keyboard then display it backwards (in terms of the words, not the letters)

I thought an array would be the most effective method so i could decrement the index to reverse, but doing that would cause my letters to be reversed not my words
My next idea was to read each word into a variable, but i do not know how to read in and skip the white space to get to the next word
Another idea was a for loop with a counter initialized at 0 and increment no larger than 3, but then my loop is infinite.
Any ideas?

I had one, its not complete but it is a work in progress

//read in sentence
cout << "Enter a 4 word sentence: ";

while (word_count !=4)
{
if (word_count < 4)
cout << "Sentence is too small";

else(word_count > 4)
cout<< "Sentence is too large";
}

if (word_count == 4)
{

}

Im pretty much stuck b/c every idea i have has a problem.
My main concern is reading in the words one by one or into an array that will allow me to reverse the words only

Experience: Some C++ (for/while loops, arrays(char), struct, str)
Last edited on
ok.. obviously that is work in progress.. as you're not showing all the code.. not initializing or setting word_count, and else(word_count > 4) should be else if(...) etc..

However. to give you an idea for looking for space.. after getting the input into a string, loop through the string one at a time and look for the space " ".. as you find them, note thier locations into an array.. you're looking for 3 spaces and the last one can't be the last char in the string.. (and the first one can't be the first char in the string..)

Once you got that, then you can use the locations of the spaces with a bit of math to use substr() method of strings to get the words.
Last edited on
You can try this code!

///////////////////////==WinXp sp2 + Vc8.0 pass==/////////////////////////////////////////////////////
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
/********************************************************************
	created:	2008/07/12
	created:	12:7:2008   9:38
	filename: 	e:\Coding\CPP_Reverse\CPP_Reverse\CPP_Reverse.cpp
	file path:	e:\Coding\CPP_Reverse\CPP_Reverse
	file base:	CPP_Reverse
	file ext:	cpp
	author:		hecan
	
	purpose:	reverse the words which was stored in the vector
*********************************************************************/

#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
	vector< string >  vsWord;
	copy(istream_iterator<string>(cin), istream_iterator<string>(), back_inserter(vsWord));//input the data: Ctrl + z  to end

	copy(vsWord.begin(), vsWord.end(), ostream_iterator<string>(cout, " "));
	cout << endl;

	reverse(vsWord.begin(), vsWord.end());//reverse all words

	copy(vsWord.begin(), vsWord.end(), ostream_iterator<string>(cout, " "));//show the results
	cout << endl;
}
Isn't this way above his stated experience as given in his post??
Isn't this way above his stated experience as given in his post??


I keep re-reading that sentance and get lost every time. what?

As per the post above that, I thought especially since this was a homework assignment that we shouldn't give them the complete code. I would do it differently, but if I give the code, I'd like to make sure you understand why it works, and not just copy and paste just for a grade.

There are two ways to do this. I was originally going to search the string using string.find() for spaces, record their locations, and then use them with string.substr() to get the words out as I printed them to cout.

I've changed my mind, however, and am now going through the string one character at a time, and parsing out the words into a string vector.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
#include <vector>

int main(int argc, char *argv[])
{
   string input; // our input string
   vector<string> s_words; // to contain our words
   string new_word; // for building the words

   cout << "Enter a 4 word sentance: ";
   getline (cin, input); // get our input

   for (int i = 0; i < input.size(); i++) // for each char in input
   {
      if (input.at(i) == " ") // if char equal to space
      {
         if (new_word.size() > 0) // if we have something in new_word
         {
            s_words.push_back(new_word); // add it to our array
            new_word = ""; // clear new_word
         }
         continue; // go to next iteration of loop
      }

      new_word.push_back(input.at(i)); // add char to new_word
   }

   if (s_words.size() != 4) // if we have other than 4 words
   {
      cout << "Incorrect number of words." << endl; // cout error
      return 0; // exit program
   }

   cout << "Reversed sentance: " << endl;

   for (int i = 3; i >= 0; i--) // for each word in reverse
      cout << s_words.at(i) << " "; // cout the word

   cout << endl; // and finally an endline

   system("PAUSE"); // pause system
   return 0;
}


I did this is notepad, and havn't compiled it, but it looks logical and should work as advertised. Just remember, I'm not a programmer. I only play one on this forum. And please make sure you understand why this code works, not just copy and paste it.
Sorry aakanaar, my post was directed at Simo110 who seem to have gone off on a tangent with vectors and other template stuff.
The original author said he knew some c++ (structs and arrays and loops)
Topic archived. No new replies allowed.