Sentence flip, such as I am Superman-> Superman am I

Source: Internet
Author: User

Sentence flip

I am Superman ---> Superman am I

 

This question is relatively simple:

Ideas:

(1) split the original string into words, and then combine the words into reverse strings;

The function is as follows:

 

# Include "stdafx. H"
# Include "stdlib. H"

# Include <string>

# Include <list>

Using namespace STD;

 

Bool inversestring (const STD: String strsrc, STD: string & strdsc)
{
 
List <string> substring;
List <string >:: iterator ITER;

Int ibegin = 0, iend = 0;

// If it is null
If (strsrc. Empty ())
{
Return false;
}
 
// Extract each sub-word string
For (unsigned int I = 0; I <strsrc. Size (); I ++)
{
// It is a space.
If (strsrc [I] = 0x20)
{
Iend = I;
}

// It is a legal substring
If (iend-ibegin)> 0)
{
STD: String tmpstr = strsrc. substr (ibegin, iend-ibegin );

Substring. push_front (tmpstr );

Ibegin = I;
}
}

 

// Last word string

STD: String tmpstr = strsrc. substr (ibegin );

Substring. push_front (tmpstr );

For (iter = substring. Begin (); iter! = Substring. End (); ITER ++)
{

Strdsc. append (* ITER );
// Add Spaces
Strdsc. append ("");

}

Return true;
}

Int main (void)
{
String strsrc = "I am Superman ";
STD: String strdesc = "";

Inversestring (strsrc, strdesc );

Printf ("DESC string is: % s \ n", strdesc. c_str ());

}

 

The above is my solution. If you have better algorithms, please let me know! I wrote this function without considering the fact that the original string contains multiple spaces!

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.