简体   繁体   English

计算换行符,空格和制表符

[英]counting newlines, spaces, and tabs

This problem is from K&R p. 这个问题来自K&R p。 20: Write a program to count blanks, tabs, and newlines. 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. 无论用户文本包含多少空格,制表符和换行符,它总是给出答案0。 Can anyone see the problem? 有谁能看到这个问题? There's one other strange thing: I have to press Ctrl-d twice for it to register. 还有一件奇怪的事情:我必须按两次Ctrl-d才能注册。 I have no idea why. 我不知道为什么。 Thanks! 谢谢!

if(character == (' ' || '\n' || '\t'))

tests whether character is equal to the result of (' ' || '\\n' || '\\t') (the result of this is 1, indicating that the result of the || is true). 测试character是否等于(' ' || '\\n' || '\\t')的结果(结果为1,表示||的结果为真)。 You need to test it individually against each of the three possible values, eg, 您需要针对三个可能值中的每一个单独测试它,例如,

if(character == ' ' || character == '\n' || character == '\t')

One of the issues you might be hitting is your condition. 你可能遇到的一个问题是你的病情。

Try something like: 尝试类似的东西:

if (character == '\n' || character == ' ' || character == '\t') {
    ++ whitespace;
}

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() != EOF which is 1 for whatever character was really read. 你赋予了字符test getchar() != EOF的值,对于任何真正读过的字符都是1。

the problem with your code is if(character == (' ' || '\\n' || '\\t')) statement. 您的代码的问题是if(character == (' ' || '\\n' || '\\t'))语句。 The statement (' ' || '\\n' || '\\t') is equivalent to 32 || 13 || 9 语句(' ' || '\\n' || '\\t')相当于32 || 13 || 9 32 || 13 || 9 32 || 13 || 9 (each character replaced by it equivalent ASCII value) which is equal to 1 as any not zero thing is consider as true in C/C++, so effectively you are doing if(character == 1) . 32 || 13 || 9 (每个字符由等效的ASCII值替换)等于1因为任何非零的东西在C / C ++中都被认为是true ,所以你有效地做了if(character == 1) Now I think you can fix the problem in your code. 现在我认为您可以解决代码中的问题。

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

isspace will 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. isspace将作为宏或函数提供,具体取决于您的系统,并且您无需再次猜测可能构成环境空白的内容。 Strictly speaking, it may be all of the following characters on your system. 严格来说,它可能是您系统上的以下所有字符。 GNU C certainly thinks so. 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++;
  }

The examples above are technically correct. 以上示例在技术上是正确的。 It prints the values only after an EOF (End Of File) indicator is called. 它仅在调用EOF (文件结束)指示符后才打印值。 However, I think there is a better explanation of this exercise (1.8), so let me purpose an alternative. 但是,我认为这个练习有更好的解释(1.8),所以让我提出一个替代方案。 The code below will print the new lines, tabs and blanks right after each new line. 下面的代码将在每个新行之后立即打印新行,制表符和空白。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM