Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C中的strtok规范化空白_C_Whitespace_Strtok - Fatal编程技术网

用C中的strtok规范化空白

用C中的strtok规范化空白,c,whitespace,strtok,C,Whitespace,Strtok,在删除子字符串后,我尝试使用strtok规范化字符串中的空白。有时,当我删除一个子串时,两个单词之间会有2个空格,我想将其减少为1,例如,删除子串rise,并从字符串中删除rome的rise and fall,在和fall之间留下2个空格 这是我迄今为止所拥有的,但我没有使用strtok的经验: char *strdel(char *string, const char *substring) { int mainlen, sublen; // finds the first

在删除子字符串后,我尝试使用strtok规范化字符串中的空白。有时,当我删除一个子串时,两个单词之间会有2个空格,我想将其减少为1,例如,删除子串rise,并从字符串中删除rome的rise and fall,在和fall之间留下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);
}

您已经很好地提供了一个简短的代码示例,但是请注意,如果您可以限制列宽和行数,那么这是一个理想的选择;我做到了。你已经明确了你的目标。但你没有弄清楚的是,你击中的关键是什么。什么台词把你弄糊涂了?当在简单示例上执行调试器时…即使是从spaceaspace中删除一个,块在哪里?最好显示调试尝试与调试我的代码的证据,并解释您不了解的具体内容。。。到while循环,使用df=strtok。。。当字母之间有超过1个空格时,我试图查找和删除空白,这就是我遇到的问题。我可以很好地删除子字符串,但我正在努力制定一个算法来寻找双空间,然后只删除其中一个。此外,我尝试使用isspace,我认为这也可以做到,但我也无法确定一个算法