简体   繁体   中英

counting newlines, spaces, and tabs

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!

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, 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( (character = getchar()) != EOF) 

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

the problem with your code is if(character == (' ' || '\\n' || '\\t')) statement. The statement (' ' || '\\n' || '\\t') is equivalent to 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) . 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. 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++;
  }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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