C replaceall Method

Source: Internet
Author: User

The replaceall method written by others is not very good on the Internet, so I feel that I have to write it myself because of the rush of time. If there is something wrong, please give me more advice.

The C code is as follows:

# Include <stdio. h> # include <malloc. h> # include <string. h> char * replaceall (char * SRC, char * Find, char * replacewith) {// If find or replace is null, return the same string as SRC. If (find = NULL | replacewith = NULL) {return strdup (SRC);} // point to the head of the replaced string. Char * afterreplacehead = NULL; // always points to the end of the new string. Char * afterreplaceindex = NULL; // The number of times the find string appears in the SRC string int COUNT = 0; int I, j, k; int srclen = strlen (SRC ); int findlen = strlen (FIND); int replacewithlen = strlen (replacewith); // point to a location of the SRC string, from which the substring is copied to afterreplaceindex, initially, the data is copied from the SRC head. Char * srcindex = SRC; // a subscript of the SRC string. From this subscript, copy the string to afterreplaceindex, which is the first character of SRC. Int cpstrstart = 0; // obtain the number of times the find string appears in the SRC string COUNT = getfindstrcount (SRC, find); // if it does not appear, return the same string as the SRC string. If (COUNT = 0) {return strdup (SRC) ;}// apply for memory afterreplacehead = afterreplaceindex = (char *) malloc (srclen + 1 + (replacewithlen-findlen) for the new string) * count); // initialize the new string memory memset (afterreplacehead, '\ 0', sizeof (afterreplacehead); for (I = 0, j = 0, K = 0; i! = Srclen; I ++) {// if the character of the find string is the same as that of the character string in SRC. If (SRC [I] = find [J]) {// If the comparison is just started, the I value is first assigned to K for saving. If (j = 0) {k = I;} // If the find string is contained in the SRC string if (j = (findLen-1) {J = 0; // copy the string before the find string in SRC to strncpy (afterreplaceindex, srcindex, I-findlen-cpstrstart + 1) in the new string ); // modify response = afterreplaceindex + I-findlen-cpstrstart + 1; // modify srcindexsrcindex = srcindex + I-findlen-cpstrstart + 1; // cpstrstartcpstrstart = I + 1; // copy the replacewith string to the new string strncpy (afterreplaceindex, replac Ewith, replacewithlen); // modify afterreplaceindexafterreplaceindex = afterreplaceindex + replacewithlen; // modify srcindexsrcindex = srcindex + findlen;} else {J ++ ;}} else {// If the find and SRC values are inconsistent during the comparison, return the saved K value to IIF (J! = 0) {I = K;} J = 0 ;}// Finally, copy the string following the last find matching string in SRC to the new string. Strncpy (afterreplaceindex, srcindex, I-cpstrstart); Return afterreplacehead;} int getfindstrcount (char * SRC, char * Find) {int COUNT = 0; char * position = SRC; int findlen = strlen (FIND); While (position = strstr (Position, find ))! = NULL) {count ++; position = Position + findlen;} return count ;}int main (void) {char * s = "12345345443344341334542345"; // call the function replaceall, S is the source string, "345" is the string to be replaced, and "7890" is the replacement string. Char * r = replaceall (S, "345", "7890"); printf ("% s \ n", R); // do not use the replaceall function, because replace contains the memory of the malloc result string. Free (r); Return 0 ;}

Running result:

127890789044334434137890427890

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.