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 ".
The solution is to first flip the entire sentence and then flip each word.
The Code is as follows:
# 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
/* This annotation is another implementation, but it seems complicated to use 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 ;}