用C中的strtok标准化空格

时间:2014-10-28 00:44:33

标签: c whitespace strtok

我在删除子字符串后尝试使用strtok来规范化字符串中的空格。有时当我删除子字符串时,两个单词之间会有2个空格,我想将其减少为1,例如删除子字符串" rise和"从字符串"罗马的兴衰"在秋天和秋天之间留下2个空格。

这是我迄今为止所拥有的,但我对strtok没有经验:

char *strdel(char *string, const char *substring) {
    int mainlen, sublen;

    // finds the first occurrence of the substring
    char *del_sub = strstr(string, substring);
    char *wspc, *df;

    while (del_sub != NULL) {
        // finds length of string from beginning of substring
        // found in main string
        mainlen = strlen(del_sub);

        // finds length of the substring
        sublen = strlen(substring);

        // removes the number of bytes needed after the
        // first instance of the substring
        memmove(del_sub, del_sub + sublen, strlen(del_sub));     

        wspc = strtok(string, "  ");

        while ((df = strtok(NULL, "  ")) != NULL) {
            strcat(string, df);
            memset(df, 0, strlen(df));
        }

        // continues to search for another occurrence of the
        // substring until it equals NULL
        del_sub = strstr(string, substring);
    }

    return(string);
}

0 个答案:

没有答案