Question:
using c++ Write a program that reads one line of text and then prints the same words in reverse order?
1970-01-01 00:00:00 UTC
using c++ Write a program that reads one line of text and then prints the same words in reverse order?
Three answers:
augustine
2016-05-28 00:03:46 UTC
Dude, I'm not going to write out a program on Y!A for you.
JDM
2008-06-12 07:12:30 UTC
To correct the answer from n4mwd, any C program is compilable as C++, so that is a valid solution to this problem.



If using the C++ Standard Template Library (STL) instead of the C Standard Library (stdlib), I would look into the following:



std::ifstream Class

http://www.cplusplus.com/reference/iostream/ifstream/



std::string Class

http://www.cplusplus.com/reference/string/string/



In particular, it's input stream operator >>

http://www.cplusplus.com/reference/string/operator%3E%3E.html



std::deque Class

http://www.cplusplus.com/reference/stl/deque/





That should be everything you need to implement this. Good luck!
cja
2008-06-12 09:07:27 UTC
Actually, it's quite easy in C++, and I think using C tokenization techniques would be more difficult. Here's my solution:



#include

#include

#include



using namespace std;



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

vector wordVec;

string line,word;

vector::iterator i;



cout << "Enter wordVec: ";

getline(cin,line);

stringstream ss(line);

while (ss.good()) {

ss >> word;

wordVec.insert(wordVec.begin(),word);

}

i = wordVec.begin();

while (i != wordVec.end()) cout << *i++ << " ";

cout << endl;

return 0;

}



If you prefer, you could use vector's push_back operation to store the words in order, then use a reverse_iterator to print them in reverse order.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...