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

来源:互联网 发布:5g网络 编辑:程序博客网 时间:2024/05/21 09:43
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;}


0 0
原创粉丝点击