c语言 除去字符串最后空格问题中while循环的问题

直接看代码

 #include <stdio.h>
//除去字符串后面的空格

int main(){
        char str[100] = "hello   world     ";
        int index = 0;
        while(str[index]){
            index++;
        }
        int i;
    /*
     * for循环写法
        for(i = index -1;i >= 0;i--){
            if(str[i] != ' '){
                str[i+1] = 0;
                break;
            }
        }
    */
        i = index - 1;
        int a = str[i] == 32;
        while(a)        //这样写能完成函数功能 但是while(str[i] == 32) 这样直接在while内判断就不行能,
                //为什么在while内判断是否为空格不行呢?大神勿喷,我是菜鸟
                //虽然在这里不是重点,直接while(str[i])或者while(1)都可以完成函数功能,钻牛角尖了
        {
            if(str[i] != ' ')
            {
                str[i + 1] = 0;
                break;
            }

            i--;
        }
    printf("(%s)\n",str);
    return 0;
}

 while(str[i] == 32)
条件满足才循环,你要的是不等于空格才循环
while(str[i] != 32)
或者如你在循环里写的
str[i] != ' '

1、能不用while循环就不要用while循环。
2、while(str[i] == 32) 是说str[i]为空格的时候才会进去。。。要改为while(str[i] !== 32)
不过这么写容易产生其他的问题。
3、从你的逻辑上来讲,你是想遍历字符串,然后碰到是空格的字符,然后将空格去掉。
分析:
a。字符串是两个对象,因为发生了操作,两个便于控制,以及代码简介
b、循环、循环退出条件是到字符串末尾、且不能超过字符串长度
c、异常需要记录日志,这里就不写了

简单写下,不注意语法了
str str_cp
for (index = 0; index < len(str); index++)
{
if (str[index] == " ")
{
continue;
}

str_cp[index] = str[index]

}

一觉醒来,突然反应过来,while(str[i] == 32)不是和if(str[i] != ' ')矛盾了吗?所以这里while循环不需要判断,直接while(1)就好了,while里有if判断,
看来熬夜没有灵感,容易钻牛角尖