Counting Tabs, New lines, etc. ~error~

Discussion in 'C Programming' started by Matt, Apr 20, 2005.

  1. Matt

    Matt Guest

    Ok, sorry for posting so much, but I just have alot of questions. Ok,
    so It says to make a program to count blanks, tabs, and newlines.

    So I made one:

    #include <stdio.h>

    /* Count tabs, newlines and blanks; version 1 */
    int main(void)
    {
    int c, nl, tb, cb;

    nl = 0;
    tb = 0;
    cb = 0;
    while ((c = getchar()) != EOF)
    if (c == '\n')
    ++nl;
    if (c == '\t')
    ++tb;
    if (c == '_')
    ++cb;
    printf("%d\n%d\n%d\n", nl, tb, cb);
    return 0;
    }

    Now here's what I don't understand:

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

    That(\t) be a tab so when I put a tab in it should make the count 1,
    but it doesn't.

    And:

    if (c == '_')
    ++cb;

    Should add a number to the blank count when you make a space, but it
    doesn't. Also I wasn't sure about what the '?' was for a blank
    space(eh, what a noob your thinking). Yes, I know: I'm a noob.

    Ok well, could someone help me? Again: sorry for posting and asking so
    much.
    Matt, Apr 20, 2005
    #1
    1. Advertising

  2. Matt

    Jason Guest

    Matt wrote:
    [ snip! ]
    > /* Count tabs, newlines and blanks; version 1 */
    > int main(void)
    > {
    > int c, nl, tb, cb;
    >
    > nl = 0;
    > tb = 0;
    > cb = 0;
    > while ((c = getchar()) != EOF)
    > if (c == '\n')
    > ++nl;
    > if (c == '\t')
    > ++tb;
    > if (c == '_')
    > ++cb;
    > printf("%d\n%d\n%d\n", nl, tb, cb);
    > return 0;
    > }


    Hint: What is the significant difference between
    the following bits of code?

    /* SAMPLE #1 */
    while ((c = getchar()) != EOF) {
    /* some stuff */
    /* some more stuff */
    }
    /* SAMPLE #2 */
    while ((c = getchar()) != EOF)
    /* some stuff */
    /* some more stuff */
    Jason, Apr 20, 2005
    #2
    1. Advertising

  3. Matt

    Matt Guest

    Wow, that's almost funny the mistake I made. Thanks alot for helping!
    Matt, Apr 20, 2005
    #3
  4. "Matt" <> writes:
    [...]
    > And:
    >
    > if (c == '_')
    > ++cb;
    >
    > Should add a number to the blank count when you make a space, but it
    > doesn't. Also I wasn't sure about what the '?' was for a blank
    > space

    [...]

    No, that increments the blank count when it sees an underscore
    character.

    The right way to do this is (you're going to slap yourself in the
    forehead when you see this):

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

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 20, 2005
    #4
  5. Matt

    Old Wolf Guest

    Matt wrote:
    > Ok, sorry for posting so much, but I just have alot of questions. Ok,
    > so It says to make a program to count blanks, tabs, and newlines.
    >
    > if (c == '_')
    > ++cb;
    >
    > Should add a number to the blank count when you make a space, but it
    > doesn't. Also I wasn't sure about what the '?' was for a blank
    > space(eh, what a noob your thinking). Yes, I know: I'm a noob.


    '_' is not a space. It's a different character. You can use
    it in variable names, eg:

    char this_is_a_char;

    If you want to check for a space, use (naturally):

    if (c == ' ')
    Old Wolf, Apr 20, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qwweeeit
    Replies:
    2
    Views:
    637
    qwweeeit
    Dec 14, 2005
  2. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    371
    Fredrik Lundh
    Aug 13, 2008
  3. DSblizzard
    Replies:
    2
    Views:
    590
    AggieDan04
    Apr 14, 2009
  4. rantingrick

    Tabs -vs- Spaces: Tabs should have won.

    rantingrick, Jul 16, 2011, in forum: Python
    Replies:
    95
    Views:
    1,796
    Roy Smith
    Jul 19, 2011
  5. John Kopanas
    Replies:
    2
    Views:
    266
    Gregory Brown
    Jan 29, 2007
Loading...

Share This Page