#include #include #include #include using namespace std; const string WHITESPACE = " \t\v\r\n"; // an input iterator that splits by newline instead of whitespace class line_iterator : public iterator { istream& in; bool eof; string data; void read() { string temp; if (getline(in, temp)) { data = temp; } else { data = string(); eof = true; } } public: line_iterator(istream& _in) : in(_in), eof(false) { read(); } line_iterator() : in(cin), eof(true) { } // end iterator line_iterator& operator++() { read(); return *this; } const string& operator*() const { return data; } const string* operator->() const { return &data; } bool operator==(const line_iterator& other) const { return eof && other.eof; } bool operator!=(const line_iterator& other) const { return !(*this == other); } }; // processes each line that is read in void process(const string& s) { if (s.size() > 0) { ostream_iterator out(cout); bool in_whitespace; size_t curr = 0; while (curr != string::npos) { size_t old_curr = curr; if (in_whitespace) { curr = s.find_first_not_of(WHITESPACE, old_curr); string sub = s.substr(old_curr, curr - old_curr); copy(sub.begin(), sub.end(), out); in_whitespace = false; } else { curr = s.find_first_of(WHITESPACE, old_curr); string sub = s.substr(old_curr, curr - old_curr); reverse_copy(sub.begin(), sub.end(), out); in_whitespace = true; } } *out++ = '\n'; } } int main() { line_iterator it(cin), eof; for_each(it, eof, process); return 0; }