Text Reverse
Problem Descriptionignatius likes to the write words in reverse. Given a single line of text which are written by Ignatius, you should reverse all the words and then output them.
Inputthe input contains several test cases. The first line of the input was a single integer T which is the number of test cases. T test Cases follow.
Each test case contains a, with several words. There'll is at the most of characters in a line.
Outputfor Each test case, you should the output of the text which is processed.
Sample Input
3olleh!dlrowm ' I Morf. Udhi Ekil. MCA
Sample Output
Hello world! I ' m from Hdu. I like ACM. Hint Remember to use GetChar () to read ' \ n ' after the Interger T, then the If use gets () to read a line and process it.
This problem can be used as an array or as a stack, and it can be done with a C string stream. Note that there may be more than 1 spaces in the middle. Keng! Pay attention to the special. Use the stack code as follows:
#include <cstdio> #include <stack> #include <cstring>using namespace Std;int main () { int t; scanf ("%d", &t); GetChar ();//absorbs carriage return while (t--) { char x; stack<char>f; while (1) { X=getchar (); if (x!= ' &&x!= ' \ n ' &&x!=eof) F.push (x); else{ while (!f.empty ()) { printf ("%c", F.top ()); F.pop (); } if (x== ' \ n ' | | x==eof) break; printf (""); } } printf ("\ n"); } return 0;}
Use the C string stream code as follows:
#include <cstdio> #include <sstream> #include <iostream>using namespace Std;int main () { int T; string S,ss; scanf ("%d", &t); GetChar (); while (t--) { getline (cin,s); int x=0; while (s[x]== ') {//may have a space at the beginning of printf (""); x + +; } Istringstream sin (s); while (SIN>>SS) { x+=ss.length (); for (int i=ss.length () -1;i>-1;i--) printf ("%c", Ss[i]); while (X<s.length () &&s[x]== ") {///middle space may not be a printf (" "); x + +; } } printf ("\ n"); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1062.Text Reverse "stack or array or string stream" "Character processing" "August 30"