A reverse string written question

Source: Internet
Author: User

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.

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.