Self-implemented C-language string replacement function

Source: Internet
Author: User

The C language does not provide a string replacement function. Similar functions that can be found on the Internet can only be replaced with one function, but not all functions. This function is often used at work, so a function is implemented. The related functions used by this function are self-implemented and no library functions are called. The related code is as follows: [html]/********************************** * Function: my_strstr () * Description: Find a substring in a string; * cballs: none; * Called By: none * Input: ps: source; pd: substring * Output: none; * Return: 0: The Source string does not contain substrings; 1: The Source string contains substrings; * Author: ChenZhiFa * Others: none; * date of completion: **************************************** * ***************************/char * my_strstr (char * ps, char * pd) {Char * pt = pd; int c = 0; while (* ps! = '\ 0') {if (* ps = * pd) {while (* ps = * pd & * pd! = '\ 0') {ps ++; pd ++; c ++ ;}} else {ps ++;} if (* pd =' \ 0 ') {// sum ++; return (ps-c);} c = 0; pd = pt;} return 0 ;} /*************************************** * ***************************** Function: memcpy () * Description: copy one memory area to another; * cballs: none; * Called By: none * Input: src: source; count: Number of copied bytes. * Output: dest: Replication destination; * Return: dest; * Author: ChenZhiFa * Others: none; * date of completion :***** **************************************** * **********************/Void * memcpy (void * dest, const void * src, size_t count) {char * tmp = (char *) dest, * s = (char *) src; while (count --) * tmp ++ = * s ++; return dest ;} /*************************************** * ***************************** Function: str_replace () * Description: searches for a substring in a string and replaces all matched substrings with another replacement string. * CILS: memcpy (); * Called By: none * Input: p_source: the parent string to be searched; p_seach: The substring to be searched; p_repstr: the replacement string; * Output: p_result: stores the result; * Return: returns the number of substrings that have been replaced successfully; * Author: ChenZhiFa * Others: p_result must be large enough to store the result, therefore, all input parameters must end with \ 0; * date of completion: **************************************** * **************************/int str_replace (char * p_result, char * p_source, char * p_seach, char * p_repstr) {int c = 0; int repstr_leng = 0; int searchstr_leng = 0; char * p1; char * presult = p_result; char * psource = p_source; char * prep = p_repstr; char * sort ach = p_seach; int nLen = 0; repstr_leng = strlen (prep); searchstr_leng = strlen (sort ACh ); do {p1 = my_strstr (psource, p_seach); if (p1 = 0) {strcpy (presult, psource); return c;} c ++; // Add 1 to the matched substring count; printf ("Result: % s \ r \ n", p_result); printf ("Source character: % s \ r \ n ", p_source); // copy the string nLen = p1-psource; memcpy (presult, psource, nLen) between the last replacement point and the next replacement point ); // copy the string memcpy to be replaced (presult + nLen, p_repstr, repstr_leng); psource = p1 + searchstr_leng; presult = presult + nLen + repstr_leng;} while (p1 ); return c;} test code: [cpp] # define MAX 200 int main (void) {int I = 0; char s [MAX] = {0 }; // store the source string char s1 [MAX] = {0}; // store the sub-string char s2 [MAX] = {0 }; // store the replacement string char result_a [2000] = {0}; // store the replacement result; char * p, * ptm, * pr; puts ("Please input the string for s:"); scanf ("% s", s); puts ("Please input the string for s1 :"); scanf ("% s", s1); puts ("Please input the string for s2:"); scanf ("% s", s2); ptm = s; pr = result_a; I = str_replace (pr, ptm, s1, s2); printf ("replace % d substring; \ r \ n", I ); printf ("result after replacement: % s \ r \ n", result_a); system ("pause");} run the result if: [cpp] Please input the string for s: 123123123123 Please input the string for s1: 23 Please input the string for s2: abcdefg result: www.2cto.com source character: 123123123123 result: 1 abcdefg source character: 123123123123 result: 1abcdefg1abcdefg source character: 123123123123 result: 1abcdefg1abcdefg1abcdefg source character: 123123123123 Replace four substrings; Result: 1abcdefg1abcdefg1abcdefg1abcdefg press any key to continue...

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.