检查角色是否是空格

时间:2021-08-11 19:11:37

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个字符。为此,我保持已输入的字符数的运行计数。一旦char计数达到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行之后输入什么字符,都会创建一个新行。我错过了什么吗?这真的意味着任何角色吗?

4 个解决方案

#1


5  

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.

我认为这应该有效。当少于80个字符但字符不是空格时,这应该阻止else执行。

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;
}

#2


4  

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

您的条件有问题:如果lineLengthCount> 70但是下一个字符不是空格,则会触发最后一个,打破行并重置计数器。

#3


1  

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”...

#4


1  

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',这个函数实际上意味着任何'空格'字符,因此包括'\ 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!

但是为了便于阅读,我宁愿使用第一个版本!

#1


5  

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.

我认为这应该有效。当少于80个字符但字符不是空格时,这应该阻止else执行。

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;
}

#2


4  

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

您的条件有问题:如果lineLengthCount> 70但是下一个字符不是空格,则会触发最后一个,打破行并重置计数器。

#3


1  

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”...

#4


1  

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',这个函数实际上意味着任何'空格'字符,因此包括'\ 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!

但是为了便于阅读,我宁愿使用第一个版本!