您的位置:首页 > 其它

字符串匹配问题

2008-10-03 20:56 225 查看
问题:给定字符串“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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: