Deletes a (first) substring from a string.

Source: Internet
Author: User

Function prototype:

int del_substr(char *str,char const *substr);

First, you should judge whether substr is in Str. If not, return 0;

If so, the function should copy all the characters behind the substring in STR to the position of the substring, delete the substring, and return 1. If the substr appears in STR multiple times, the function only deletes the substring that appears for the first time. The second parameter of the function should never be modified.

Source code:

Int del_substr (char * STR, char const * substr) {If (STR = NULL | substr = NULL) {return 0 ;} // mark the start position of substr in STR as char * begin = STR; // mark the end position of substr in STR as char * end = STR; // substr cursor pointer char const * Index = substr; while (* begin! = '\ 0' & * end! = '\ 0') {// search for substrwhile (* begin! = * Index) {begin ++;} End = begin + 1; index ++; while (* Index = * end & * index! = '\ 0' & * end! = '\ 0') {index ++; end ++ ;}// if both index and end reach the end of the respective string, the substr substring is found in Str. // For example, STR: "abcdexyz", substr: "XYZ" If (* Index = '\ 0' & * end =' \ 0') {* begin = '\ 0'; printf (STR ); return 1;}/* If the index reaches the end of The substr string, but the end does not reach the end of the STR, The substr substring is found in Str * For example, STR: "abcdexyzmn", substr: "XYZ" */else if (* Index = '\ 0' & * end! = '\ 0') {While (* begin ++ = * end ++); printf (STR); return 1 ;}/ * If the index does not reach the end of the substr string, the end has reached the end of STR, and the substr substring is not found in Str * For example, STR: "abcdexyzmn", substr: "xyzmnst" */else if (* index! = '\ 0' & * end =' \ 0') {printf (STR); Return 0;}/* string matching has not been completed * For example, STR: "xyzabcdexyzmnstghd ", substr: "xyzmnst" */else {begin ++; end = begin; Index = substr ;}} printf (STR); Return 0 ;}

If you need to delete all substr in STR that contains multiple substr, you can use the above function for recursive calling.

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.