C Language Implementation String substitution

Source: Internet
Author: User

voidReplacefirst (Char*STR1,Char*STR2,Char*STR3) {      CharStr4[strlen (STR1) +1]; Char*p;      strcpy (STR4,STR1); if((P=strstr (STR1,STR2))!=null)/*p points to the first occurrence of str2 in str1*/      {           while(Str1!=p&&str1!=null)/*move the str1 pointer to the position of P*/{str1++; } str1[0]=' /';/*Change the value of the str1 pointer to/0 to truncate STR1, discard str2 and future content, and retain only str2 previous content*/strcat (STR1,STR3);/*str1 after the STR3, forming a new str1*/strcat (Str1,strstr (STR4,STR2)+strlen (STR2));/*strstr (STR4,STR2) is a point str2 and later (including STR2), Strstr (STR4,STR2) +strlen (STR2) is the pointer moving forward strlen (str2) bit, skipping str2*/      }  }  /*Replace all str2 that appear in str1 with STR3*/  voidStr_replace (Char*STR1,Char*STR2,Char*STR3) {  //if the passed in Parameters str2 and STR3 the same direct exit avoid while dead loop    if(!strcmp (STR2,STR3)) {        return ; }     while(STRSTR (STR1,STR2)! =NULL)      {Replacefirst (STR1,STR2,STR3); }  }  

C Language Implementation String substitution

Related Article

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.