C语言 计算换行符、空格和制表符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3221031/
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
counting newlines, spaces, and tabs
提问by Bill
This problem is from K&R p. 20: Write a program to count blanks, tabs, and newlines.
这个问题来自 K&R p。20:编写一个程序来计算空格、制表符和换行符。
Here's my attempt:
这是我的尝试:
#include <stdio.h>
int main()
{
int character, whitespace = 0;
printf("Enter some text, and press Ctrl-d when you're done.\n\n");
while((character = getchar() != EOF) {
if(character == (' ' || '\n' || '\t')) {
++whitespace;
}
}
printf("\nYour text contains %d spaces, tabs, and lines.\n", whitespace);
return 0;
}
The program doesn't work. It always gives the answer 0 no matter how many spaces, tabs, and newlines the user text contains. Can anyone see the problem? There's one other strange thing: I have to press Ctrl-d twice for it to register. I have no idea why. Thanks!
该程序不起作用。无论用户文本包含多少空格、制表符和换行符,它始终给出答案 0。任何人都可以看到问题吗?还有一件奇怪的事情:我必须按 Ctrl-d 两次才能注册。我不知道为什么。谢谢!
回答by James McNellis
if(character == (' ' || '\n' || '\t'))
tests whether characteris equal to the result of (' ' || '\n' || '\t')(the result of this is 1, indicating that the result of the ||is true). You need to test it individually against each of the three possible values, e.g.,
测试是否character等于的结果(' ' || '\n' || '\t')(this的结果为1,表示the的结果||为真)。您需要针对三个可能的值中的每一个单独测试它,例如,
if(character == ' ' || character == '\n' || character == '\t')
回答by NG.
One of the issues you might be hitting is your condition.
您可能遇到的问题之一是您的状况。
Try something like:
尝试类似:
if (character == '\n' || character == ' ' || character == '\t') {
++ whitespace;
}
回答by Patrick Schlüter
Parenthesis in your while statement is wrong, it should be
你while语句中的括号是错误的,应该是
while( (character = getchar()) != EOF)
You assigned to character the value of the test getchar() != EOFwhich is 1 for whatever character was really read.
您将测试的值分配给字符,getchar() != EOF对于实际读取的任何字符,该值都是 1。
回答by Ravi Gupta
the problem with your code is if(character == (' ' || '\n' || '\t'))statement. The statement (' ' || '\n' || '\t')is equivalent to 32 || 13 || 9(each character replaced by it equivalent ASCII value) which is equal to 1as any not zero thing is consider as truein C/C++, so effectively you are doing if(character == 1). Now I think you can fix the problem in your code.
您的代码的问题是if(character == (' ' || '\n' || '\t'))语句。该语句(' ' || '\n' || '\t')等效于32 || 13 || 9(每个字符替换为它等效的 ASCII 值),它等于C/C++ 中1考虑的任何非零事物true,因此您正在有效地执行if(character == 1). 现在我认为您可以解决代码中的问题。
Also books says to count blanks, tabs, and newlines separately and you are trying to count the total numbers, so do something like this.
书籍还说要分别计算空格、制表符和换行符,而您正在尝试计算总数,因此请执行以下操作。
if(character == ' ')
++blanks;
if(character == '\t')
++tabs;
if(character == '\n')
++newlines;
If you want a complete solution, here is one which i had written a long time back.
如果你想要一个完整的解决方案,这是我很久以前写的一个。
#include <stdio.h>
int main(void)
{
int blanks, tabs, newlines;
int c;
blanks = 0;
tabs = 0;
newlines = 0;
do {
c = getchar();
if(c == ' ') {
++blanks;
}
else if(c == '\t') {
++tabs;
}
else if(c == '\n') {
++newlines;
}
}
while(c != EOF)
printf("Blanks: %d\nTabs: %d\nLines: %d\n", blanks, tabs, newlines);
return 0;
}
回答by Plar625
The examples above are technically correct. It prints the values only after an EOF(End Of File) indicator is called. However, I think there is a better explanation of this exercise (1.8), so let me purpose an alternative. The code below will print the new lines, tabs and blanks right after each new line.
上面的例子在技术上是正确的。它仅在EOF调用 (End Of File) 指示器后打印值。但是,我认为对这个练习 (1.8) 有更好的解释,所以让我提出一个替代方案。下面的代码将在每个新行之后打印新行、制表符和空格。
#include <stdio.h>
#define EOL '\n'
/* Excercise 1.8 - K&R's book - 2nd edition. */
main()
{
int c, newlines, tabs, blanks;
newlines = 0;
tabs = 0;
blanks = 0;
while ((c = getchar()) != EOF)
{
if (c == '\n')
++newlines;
else if (c == '\t')
++tabs;
else if (c == ' ')
++blanks;
if (c == EOL) {
printf("Lines: %d\nTabs: %d\nBlanks: %d\n", newlines, tabs, blanks);
}
}
}
回答by bjg
isspacewill be available as a macro or function depending on your system and saves you having to second guess what might constitute whitespace in your environment. Strictly speaking, it may be all of the following characters on your system. GNU C certainly thinks so.
isspace将作为宏或函数使用,具体取决于您的系统,并且您不必再猜测什么可能构成您的环境中的空白。严格来说,它可能是您系统上的以下所有字符。GNU C 当然是这么认为的。
' '
space
'\f'
formfeed
'\n'
newline
'\r'
carriage return
'\t'
horizontal tab
'\v'
vertical tab
This is how you can do the test.
这就是您可以进行测试的方式。
#include <ctype.h>
while((character = getchar() != EOF) {
if (isspace(character)) whitespace++;
}

