Enter an English sentence to flip the order of words in the sentence. The character sequence in the word remains unchanged.

Source: Internet
Author: User

[Cpp]

<P> question: enter an English sentence to flip the order of words in the sentence, but the order of Characters in the word remains unchanged. Words in a sentence are separated by space characters.
For simplicity, punctuation marks are processed like normal letters.
For example, if "I am a student." Is input, "student. a am I" is output ". </P> <p> the solution is to first flip the entire sentence and then flip each word. </P> <p> code: </p>
[Cpp]

# Include <iostream>
 
Using namespace std;
 
Void myReverse (char *, char *);
Char * senReverse (char *);
Int main ()
{
Char sen [] = "hello, I am a student! ";
Char * result = senReverse (sen );
Cout <result <endl;
Return 0;
}
Void myReverse (char * start, char * end)
{
Char temp;
While (start <end)
{
Temp = * start;
* Start = * end;
* End = temp;
Start ++;
End --;
}
}
Char * senReverse (char * sen)
{
Char * start = sen, * end = sen + strlen (sen)-1;
MyReverse (start, end); // first reverse the entire sentence

[Cpp]
The release part is another implementation, but it seems complicated.
Int loc; // record the length of each word
For (start = sen, end = start; (* end )! = '\ 0'; start + = loc)
{
Loc = 0;
While (* end )! = ''& (* End )! = '\ 0') {end ++, loc ++ ;}
End --;
MyReverse (start, end );
End + = 2;
Loc ++;
}
*/
End = start;
While (* start! = '\ 0 ')
{
If (* end = ''| * end = '\ 0 ')
{
MyReverse (start, -- end );
If (* end = '\ 0 ')
Break;
End + = 2;
Start = end;
}
Else
++ End;
}
 
Return sen;

Related Article

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.