现在的位置: 首页 > 综合 > 正文

C语言删除字符串中的指定字符串

2018年02月14日 ⁄ 综合 ⁄ 共 591字 ⁄ 字号 评论关闭
void replace_string(char * source_str,char * targ_str,char *val)/*将字符串中指定子字符串用指定字符串代替,targ_str 是被替换的,val是替换的字符串*/
{
    char temp_sstr[513],result[513];
    char * p,*q;
    int len;len=0;q=p=NULL;
    memset(result,0,sizeof(result));
    memset(temp_sstr,0,sizeof(temp_sstr));
    strcpy(temp_sstr,source_str);
    p=q=temp_sstr;
    len=strlen(targ_str);
    while(q!=NULL)
    {
        if((q=strstr(p,targ_str))!=NULL)
            {
                strncat(result,p,q-p);
                strcat(result,val);
                strcat(result,"\0");
                q+=len;
                p=q;
            }
        else 
            strcat(result,p);
    }
    strcpy(source_str,result);
}

调用:

int main()
{
        char s[123];
        strcpy(s, "cert: \\n");
        printf("%s\n", s);
        replace_string(s, "\\n","\n");
        printf("%s\n", s);
        return 0;
}

抱歉!评论已关闭.