C语言 检查字符是否为空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15326729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:39:30  来源:igfitidea点击:

Check if a character is a space

c

提问by user1427661

I'm creating an absurdly simple program in C to mess around with getchar(). The program will print out what you input until you press enter and it will guarantee that your lines are no more than 80 chars each. To do this, I keep a running count of how many characters have been entered. Once the char count hits 70, the next space encountered will cause a line break. If no space is encountered between 70-80, a line break will occur regardless. I realize this is a super naive implementation and could be optimized left and right, but remember, I'm just messing around:

我正在用 C 创建一个非常简单的程序来处理getchar(). 该程序将打印出您输入的内容,直到您按下 Enter 键,并保证您的每行不超过 80 个字符。为此,我会持续计算已输入的字符数。一旦字符计数达到 70,下一个遇到的空格将导致换行。如果在 70-80 之间没有遇到空格,则无论如何都会发生换行。我意识到这是一个超级幼稚的实现,可以左右优化,但请记住,我只是在胡闹:

while ((c = getchar()) != '\n') {
    if (lineLengthCount < 70) {
        putchar(c);
        lineLengthCount++;
    }   
    else if (lineLengthCount < 80 && (c == ' ')) {
        printf("%c\n", c); 
        lineLengthCount = 0;
    }   
    else {
        printf("%c\n", c); 
        lineLengthCount = 0;
    }   
}   

The problem is the c == ' 'conditional doesn't seem to be actually checking for a space. I get output like this:

问题是c == ' '条件似乎实际上并没有检查空格。我得到这样的输出:

fitter happier more productive comfortable not drinking too much regula
r exercise at the gym three days a week getting on better with your ass
ociate employee contemporaries at ease eating well no microwaved dinner

where I was hoping that the lines would be truncated when a space was encountered. Instead, no matter what character is entered after line 70, a new line is created. am I missing something? Does ' 'really mean any character?

我希望在遇到空格时这些行会被截断。相反,无论在第 70 行之后输入什么字符,都会创建一个新行。我错过了什么吗?难道' '真的是任何字符?

回答by Metabble

while ((c = getchar()) != '\n') {
    if (lineLengthCount < 70) {
        putchar(c);
        lineLengthCount++;
    }   
    else if (lineLengthCount < 80 && (c == ' ')) {
        printf("%c\n", c); 
        lineLengthCount = 0;
    }   
    else if (lineLengthCount >= 80){
        printf("%c\n", c); 
        lineLengthCount = 0;
    }
    else{
        putchar(c);
        lineLengthCount++;
    }
}

I think this should work. That should prevent the else from executing when there are less than 80 characters but the character isn't a space.

我认为这应该有效。这应该可以防止 else 在少于 80 个字符但字符不是空格时执行。

EDIT: I realized now that instead if lineLengthCount is less than 80 but the character isn't a space it wouldn't get printed at all, so I added another else at the end to fix it.

编辑:我现在意识到,如果 lineLengthCount 小于 80 但字符不是空格,它根本不会被打印出来,所以我在最后添加了另一个来修复它。

Wouldn't this be shorter and more concise?

这不是更短更简洁吗?

while ((c = getchar()) != '\n') {
    putchar(c);
    if((c == ' ' && lineLengthCount >= 70) || lineLengthCount >= 80){
        printf("\n");
        lineLengthCount = 0;
    }
    else
        ++lineLengthCount;
}

回答by molnarm

There's a problem with your conditions: if lineLengthCountis > 70 but the next character is not a space, the last elsewill be hit, breaking line and resetting the counter.

你的条件有问题:如果lineLengthCount> 70 但下一个字符不是空格,最后一个else将被击中,断行并重置计数器。

回答by KevinM

If you're at all unsure of what's going on, I would recommend breaking up the "if" conditional into three explicit checks:

如果您完全不确定发生了什么,我建议将“if”条件分解为三个显式检查:

while ((c = getchar()) != '\n') {
    lineLengthCount++;
    if (lineLengthCount < 70) {
        putchar(c);
    }   

    if (lineLengthCount < 80 && (c == ' ')) {
        printf("%c\n", c); 
        lineLengthCount = 0;
    }   

    if (lineLengthCount == 80) {
        printf("%c\n", c); 
        lineLengthCount = 0;
    }   
}   

If you want to see what's happening, write some debugging output in each "if" to notice when it is called.

如果你想看看发生了什么,在每个“if”中写一些调试输出来注意它什么时候被调用。

Once it works, and you understand why, you can edit it down and combine the "ifs" ...

一旦它起作用,并且你明白为什么,你可以编辑它并结合“ifs”......

回答by tom1990

Using ' ' is completely valid. You could also try using the C standard library function isspace() to check if the character is a space. This function returns a boolean expression, As in:

使用“ ”是完全有效的。您也可以尝试使用 C 标准库函数 isspace() 来检查字符是否为空格。此函数返回一个布尔表达式,如:

char ch = '0';

if (isspace(ch))
    //the char is a space...

By 'is space' this function actually means is any 'whitespace' character, so that includes '\n' or any other character that prints as empty space.

通过'is space',这个函数实际上意味着是任何'whitespace'字符,因此包括'\n'或任何其他打印为空白的字符。

You could also the decimal value 32 which means the same as a space:

您还可以使用十进制值 32,这意味着与空格相同:

if (ch==32)

However for readability I would rather use the first version!

但是为了可读性,我宁愿使用第一个版本!