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