Remove characters from a string in C

后端 未结 4 1161
生来不讨喜
生来不讨喜 2020-12-10 08:34

I only have access to \'C\' and need to replace characters within a character array. I have not come up with any clean solutions for this relatively simple procedure.

4条回答
  •  孤街浪徒
    2020-12-10 08:55

    char *s = (char*)strBuffer;
    char sClean[strlen(strBuffer) + 1]; /* +1 for null-byte */
    /* if above does not work in your compiler, use:
        char *sClean = (char*)malloc(sizeof(strBuffer) + 1);
    */
    int i=0;
    while (*s)
    {
        sClean[i++]= *s;
        if ((*s == '&') && (!strncmp(s, "&", 5)) s += 5;
        else s++;
    }
    sClean[i] = 0;
    

提交回复
热议问题