且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 C 中的字符串/字符数组中删除空格的函数

更新时间:2022-12-28 16:50:50

input 中删除空格后,您还没有使用 nul-terminator (\0>) 因为新的长度小于或等于原始字符串.

After removing the white spaces from the input you have not terminated it with nul-terminator (\0) because the new length is less than or equal to the original string.

只需在 for 循环结束时将其终止即可:

Just nul-terminate it at the of end your for loop:

char* deblank(char* input)                                         
{
    int i,j;
    char *output=input;
    for (i = 0, j = 0; i<strlen(input); i++,j++)          
    {
        if (input[i]!=' ')                           
            output[j]=input[i];                     
        else
            j--;                                     
    }
    output[j]=0;
    return output;
}