tags:

views:

106

answers:

5

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!

+2  A: 

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

Try something like:

if (character == '\n' || character == ' ' || character == '\t') {
    ++ whitespace;
}
SB
+12  A: 
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')
James McNellis
Expanding - this is a case of an English shorthand that simply doesn't apply in most programming languages. `(a == b) || (a == c)` is not the same as `(a == (b || c))` - the latter is often a bug. Note that the semantics you're intending for `(b || c)` is building a list - in Python you could write IIRC `if a in [b, c, d] :`, but it's still not just a shorthand for the conditional expression you want.
Steve314
A: 

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++;
  }
bjg
This solves a different problem (it counts extra characters beyond the ones in the problem). -1 for not reading the standard documentation - `isspace` is defined to return true for **exactly** that set of characters, plus potentially others in locales other than the `C` locale (but never fewer).
R..
While I agree it solves a different problem (a superset of the one asked) I don't see that as injurious to health but rather to be likely of some benefit to an ab initio C programmer - perhaps encouraging some thinking outside the problem posed. How you can conclude that my answer suggests that I didn't read the standard documentation is more curious. In fact, to be able to conclude that **something didn't happen** without evidence quite is intriguing. Perhaps you could elaborate on how you came to such a novel conclusion? -1 for crass argumentation I would suggest.
bjg
+2  A: 

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.

tristopia
A: 

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;
  int done = 0;
  int lastchar = 0;

  blanks = 0;
  tabs = 0;
  newlines = 0;

  while(done == 0)
  {
    c = getchar();

    if(c == ' ')
      ++blanks;

    if(c == '\t')
      ++tabs;

    if(c == '\n')
      ++newlines;

    if(c == EOF)
    {
      if(lastchar != '\n')
        ++newlines; 
      done = 1;
    }
    lastchar = c;
  }

  printf("Blanks: %d\nTabs: %d\nLines: %d\n", blanks, tabs, newlines);
  return 0;
}
Ravi Gupta