For example: to transpose a string, the words in the string must be normal.
For example:
Given S ="the sky is blue",
Return"blue is sky the".
Note:
1. The input string may contain leading spaces and trailing spaces. The output must not contain leading spaces or trailing spaces.
2. There may be multiple spaces between two words in the input statement, and only one space is allowed between two words.
# Include <iostream> # include <string> using namespace STD; void reverse (string & S, int start, int end) {While (start <End) {char TEM = s [start]; s [start] = s [end]; s [end] = TEM; Start ++; end --;}} void reversestring (string & S) {If (S. empty () | S. length () = 0) {return;} // remove the leading and trailing spaces int num1 = S. find_first_not_of (""); If (num1 =-1) {S = ""; return;} int num2 = S. find_last_not_of (""); s = S. substr (num1, num2-num1 + 1); int start = 0, end = 0; int size = S. length (); End = size-1; // the entire string is inverted reverse (S, start, end); // If there are multiple spaces between two words, int COUNT = 0; for (INT I = 0; I <size; I ++) {If (s [I] = ''& S [I + 1] = '') continue; s [count ++] = s [I];} If (count <size) S = S. substr (0, count); size = S. size (); Start = 0; end =-2; // each word is inverted for (INT I = 0; I <size; I ++) {If (s [I] =='') {start = end + 2; end = I-1; reverse (S, start, end );} if (I = size-1) {start = end + 2; end = I; reverse (S, start, end) ;}} int main (INT argc, char ** argv) {string S = "a B"; cout <"before reverse:" <S <Endl; reversestring (s ); cout <"after reverse:" <S <Endl; return 0 ;}
Transposed string, where the characters in the word must be normal