问题:给定字符串“This is a string”,删除字符串中的“is”。
代码:
- /*return the remainder string*/
- char * matchstr(const char * src,const char *substr)
- {
- const char * p = substr;
- int i = 0;
- int src_len = strlen(src);
- int sub_len = strlen(substr);
- int buf_len = src_len;
- char *tmp_buf = new char[src_len+1];
- char * buf = NULL;
- while(*src!='/0')
- {
- const char * ptmp = strstr(src,p);
- if(ptmp!=NULL){
- while(src<ptmp)//copy strings which is befor ptmp postion into tmp_buf.
- {
- tmp_buf[i]=*src;
- ++i;
- ++src;
- }
- src = src+sub_len;
- }else{//if not match substr in src,then copy all string into tmp_buf
- while(*src!='/0')
- {
- tmp_buf[i]=*src;
- ++i;
- ++src;
- }
- }
- }
- tmp_buf[i]='/0';
- buf_len = strlen(tmp_buf);
- buf = new char[buf_len+1];
- strcpy(buf,tmp_buf);
- delete[] tmp_buf;
- return buf;
- }