[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;