C ++ engineers to develop a daily topic fourth (use reverse of)

topic:

The words were inverted sentence, punctuation is not upside down.

 

 This question is the easiest solution is to use the reverse algorithm provided by () function

Concrete steps which I have written in code comments:

. 1 #include < String >
 2 #include <algorithm>
 . 3  int main () {
 . 4      String S;
 . 5      getline (cin, S);       // string included in the input space, we can not use the input cin (space will encounter stop), using the characters read all getline (newline, EOF, custom symbol stops) 
. 6      Auto s.begin IT = ();   // a body I selected string traversal iterator 
. 7      Reverse (S. the begin (), s.end ());     // Step first set against the entire string, and then were then each word Retrograde 
. 8      the while (! IT = s.end ()) {
 . 9          Auto II = IT;
 10          the while (II s.end = () && * II =!! '  ' ) {    // find the position of a word ending 
. 11              II ++ ;
 12 is          }
 13 is          Reverse (IT, II);                     // inverse current word counter 
14          IF (II =! S.end ()) {          
 15              IT = II + . 1 ;                // Re define the position of the beginning of a word, the word + 1 because only between a '' 
16              II ++ ;
 . 17          }
 18 is          the else {                          // if the current position is the last one, then the end condition is administered directly to the end of the cycle 
. 19              IT = s.end ( );
 20          }
 21      }
 22     << S COUT;         // output 
23 is      return  0 ;
 24 }

The most important point is the familiar reverse () use

But just on this questions is still better solution, I also found other heavyweights written after the finish, out for everyone to share here:

 1 #include<iostream>
 2 using namespace std;
 3 #include<string>
 4 int main()
 5 {
 6     string s1;
 7     string s2;
 8 
 9     cin >> s1;
10 
11     while (cin >> s2&&getchar() != '\n')
12     {
13         s1 = s2 + " " + s1;
14     }
15 
16     cout << s2 << " " << s1 << endl;
17     return 0;
18 }

After reading means I really dish. . . .

Guess you like

Origin www.cnblogs.com/Kaniso-Vok/p/11832459.html