Time Limit: 2 seconds memory limit: 65536 KB
For each list of words, output a line with each word reversed without changing the order of the words.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. each input block is in the format indicated in the Problem description. there is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Input
You will be given a number of test cases. the first line contains a positive integer indicating the number of cases to follow. each case is given on a line containing a list of words separated by one space, and each word contains only uppercase and lowercase letters.
Output
For each test case, print the output on one line.
Sample Input
1
3
I am happy today
To be or not to be
I want to win the practice contest
Sample output
I ma yppa yadot
Ot EB Ro ton ot EB
I tnaw ot NIW EHT ecitcarp tsetnoc
Source:East central North America 1999, practice
This is a question about output format control. The reverse function can be used directly in VC 10.0. in earlier versions, # include <algorithm> is required,CodeAs follows:
# Include <iostream>
# Include <sstream>
# Include <algorithm>
Using NamespaceSTD;
IntMain ()
{
IntBlocks; CIN> blocks;
For(IntBlock = 0; block <blocks; block ++)
{
StringState;
If(Block = 0)// There are only the following two blank lines at the first time
{
Getline (CIN, State );// Use Getline to eat the carriage return after the input Blocks
Getline (CIN, State );// Empty rows required by the question
}
IntStatecount; CIN> statecount;
Getline (CIN, State );
While(Statecount --)
{
Getline (CIN, State );
Istringstream is (State );
StringWord;
IntI = 0;
While(Is> word)
{
Reverse (word. Begin (), word. End ());
If(I! = 0)
Cout <" ";
Cout <word;
I ++;
}
Cout <Endl;
}
If(Block! = Blocks-1)// No blank lines are output at the last time
{
Cout <Endl;
}
}
Return0;
}
The reverse function is the key to solving this problem, which is defined in the algorithm files. Here is the reverse order function of string written by myself:
VoidReversestring (String& Str)
{
Size_t strlen = Str. Length ();
For(Size_t I = 0; I <strlen/2; I ++)
{
STR [I] = STR [I] ^ STR [strlen-I-1];
STR [strlen-I-1] = STR [I] ^ STR [strlen-I-1];
STR [I] = STR [I] ^ STR [strlen-I-1];
}
}
The test code is as follows:
# Include <iostream>
# Include <String>
Using NamespaceSTD;
VoidReversestring (String& Str)
{
Size_t strlen = Str. Length ();
For(Size_t I = 0; I <strlen/2; I ++)
{
STR [I] = STR [I] ^ STR [strlen-I-1];
STR [strlen-I-1] = STR [I] ^ STR [strlen-I-1];
STR [I] = STR [I] ^ STR [strlen-I-1];
}
}
IntMain ()
{
StringA ="ABC";
Cout <"Befor reverse, A is"<A <Endl;
Reversestring ();
Cout <"After reverse, now a is"<A <Endl;
Return0;
}
Output result: