counting newlines, spaces, and tabs

2019-05-26 02:02发布

问题:

This problem is from K&R p. 20: Write a program to count blanks, tabs, and newlines.

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!

回答1:

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). You need to test it individually against each of the three possible values, e.g.,

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


回答2:

One of the issues you might be hitting is your condition.

Try something like:

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


回答3:

Parenthesis in your while statement is wrong, it should be

while( (character = getchar()) != EOF) 

You assigned to character the value of the test getchar() != EOF which is 1 for whatever character was really read.



回答4:

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 1 as any not zero thing is consider as true in C/C++, so effectively you are doing 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;
}


回答5:

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. Strictly speaking, it may be all of the following characters on your system. GNU C certainly thinks so.

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


回答6:

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.

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


标签: c whitespace