How to reverse input words from std::cin in C++

1 Answer

0 votes
#include <iostream>  
#include <cctype>

std::istream &reverse_word(std::istream &in, std::ostream &out)
{
	char ch;

	if (in.get(ch) && !std::isspace(ch))
	{
		reverse_word(in, out);
		out << ch;
	}

	return in;
}

int main()
{
	for (unsigned i = 0; i < 3; i++)
	{
		reverse_word(std::cin, std::cout);
		std::cout << std::endl;
	}
}


/*
run:

c++
++c
python
nohtyp
java
avaj

*/

 



answered Jan 15, 2018 by avibootz

Related questions

1 answer 160 views
1 answer 85 views
1 answer 237 views
237 views asked Apr 10, 2018 by avibootz
1 answer 130 views
130 views asked Apr 9, 2018 by avibootz
1 answer 79 views
3 answers 162 views
...