RE: [Dev-C++] Simplification for my code please
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: P.D. v. Z. <pa...@my...> - 2003-10-19 07:39:12
|
Hey,
This would do the trick in a much shorter manner:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string strInput;
cout << "Original Name: ";
getline(cin, strInput);
istringstream strParse(strInput);
string::reverse_iterator it;
cout << "Reversed Name: ";
do
{
strParse >> strInput;
for (it = strInput.rbegin(); it != strInput.rend(); ++it)
cout << *it;
cout << " ";
} while (!strParse.eof());
cout << endl;
}
Note: I didn't put any "keep the window open" code in there, since I think
it's useless, and always run console apps from the command line anyway.
Regards,
Paul
________________________________
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Coosa
Sent: Saturday, October 18, 2003 2:20 PM
To: dev...@li...
Subject: [Dev-C++] Simplification for my code please
Importance: High
Sensitivity: Confidential
Hi all,
I had today a lab test in C++ and one of the 10 questions was to write a
program which accepts a string input and reverse the words separately
between spaces and outputs it to the stream. I couldn't solve it in time and
I lost the marks for this section and went home and solved it.
As a complain I want to know first, if this code could be simplified more as
compact as possible, then write my lecturer a complain that the time wasn't
enough to solve such questions, any way, the code is attached and working
fine and I tried to make it as compact as possible, so please if some one
can make it shorter, I'll be thankful.
Best Regards
Coosa
Program output example:
The words separated between spaces are isolated first and then reversed, not
the entire string!
Original Name : This is a test!
Reversed Name: sihT si a !tset
|