Reverse a sentence.

Reverse a sentence. For example: “I am a student.”==>”student. a am I”. 

C++:
01 void reverse(char* start, char* end)
02 {
03     while(start<end){
04         char tmp=*start;
05         *start=*end;
06         *end=tmp;
07         start++;
08         end;
09     }
10 }
11 
12 void sentenceReverse(char* p)
13 {
14     if(p==NULL)
15         return;
16     char* q=p;
17     while(*q!=)
18         q++;
19     q;
20     reverse(p,q);
21     char* start=p;
22     char* end=p;
23     while(true){
24         while((*start==‘ ‘)&&(*end==‘ ‘)){
25             start++;
26             end++;
27         }
28         while((*end!=‘ ‘)&&(*end!=))
29             end++;
30         end;
31         reverse(start,end);
32         start=end+1;
33         end=start;
34         if(*start==)
35             break;
36     }
37 }
Advertisements
This entry was posted in String. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s