Transposed string, where the characters in the word must be normal

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.