A student's written questions on the Internet are as follows:
Function implementation performs the following operations on the website
Convert www.google.com to com. Google. WWW and mail.netease.com to com. Netease. Mail.
STL is not allowed. The space is 0 (1)
The idea is as follows:
1. Each word separated by '.' is sorted in reverse order.
2. Re-sort the entire string in reverse order.
// Reverse the inum character of PTR starting from istart <br/> void reverse_word (char * PTR, unsigned int istart, unsigned int inum) <br/>{< br/> char TMP; <br/> for (INT I = istart; I <istart + inum/2; ++ I) <br/> {<br/> TMP = PTR [I]; <br/> PTR [I] = PTR [2 * istart + iNum-1-i]; <br/> PTR [2 x istart + iNum-1-i] = TMP; <br/>}</P> <p> void reverse (char * PTR) <br/>{< br/> int I = 0; <br/> int last_mark =-1; <br/> while (PTR [I]! = '/0') <br/>{< br/> If (PTR [I] = '. ') <br/>{< br/> reverse_word (PTR, last_mark + 1, i-last_mark-1); <br/> last_mark = I; <br/>}< br/> + + I; <br/>}< br/> reverse_word (PTR, last_mark + 1, i-last_mark-1 ); <br/> // reverse the entire string again <br/> reverse_word (PTR, 0, I); <br/>}</P> <p> int main () <br/>{< br/> char STR [50]; <br/> memset (STR, 0, sizeof (STR); <br/> strcpy (STR, "mail.netease.com"); <br/> printf ("% s/n", STR); <br/> reverse (STR ); <br/> printf ("% s", STR); <br/> return 0; <br/>}< br/>
In order to express clearly, code in reverse order is encapsulated into a function. I applied for three variables with the complexity of O (1). Of course, if we operate on pointers instead of subscripts, we will use fewer variables, but we don't have to worry about these details.