Thread: CountingWords

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    35

    CountingWords

    This is the code :
    Code:
    #include <stdio.h>
    
    #define IN 1  /* Inside a word */
    #define OUT 0 /* Outside a word */
    
    
    main()
    {
    
    
    
    
        int c, nl, nw, nc, state;
    
    
        state = OUT;
        nl = nw = nc = 0;
        while ((c = getchar()) != EOF) {
            ++nc;
            if (c == '\n')
                ++nl;
            if (c == ' ' || c == '\n' || c == '\t')
                state = OUT;
            else if (state == OUT) {
                state = IN;
                ++nw;
            }
    
    
        }
        printf ("%d %d %d\n",nl, nw, nc);
    
    
    }
    Ok,so what i don't understand (i recently started programming) is IN and OUT.I know how the symbolic constants work.What i don't get is the code.IN and OUT (outside ,inside the word).I know what define does exactly.But how do these 2 constants become inside and outside a word.I preety much don't understand anything about the IN and OUT now.Everything else is understandable in this code.
    But is there anyone who can explain to me how the IN and OUT are functioning???
    Thanks,Filster7(Sorry to bother) !

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #define IN 1  /* Inside a word */
    #define OUT 0 /* Outside a word */
    IN and OUT are states - as the CP (current position) of the file pointer is traveling through the stream of words, at that particular time, is the file pointer INside a word, or is it OUTside a word.

    If it's INside, then when it reaches the end of the current word, is will increment the word counter. If it's OUTside, then it can't count a word, no matter how many spaces or punctuation marks it lands on.

    Think of it light a light switch - it's either on or off. If it's ON, then you can't turn it on again. You can only turn it to the OFF state. When it's OFF, you can only turn it ON.

    No bother at all.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    35
    Thanks for that,but i still don't understand something.
    I tried replacing IN and OUT with random words and i changed the numbers randomly and the program was still running fine (ofcourse i changed the whole code that was needed to be changed).
    Last edited by Filster7; 07-18-2013 at 05:50 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The compiler doesn't care what you call your variables or constants. We pick names that make sense to us humans, so we can more easily read and reason about what the code is doing. The compiler also doesn't care about the specific numeric values of the states -- those values are arbitrary, and have no intrinsic meaning regarding whether you are "in" or "out" of a word. It's only important that the values be distinct (i.e. you can't use 1 to represent both being "in" and "out" of a word -- the logic would break). The fact that you gave sensible names (IN and OUT) allows you as a programmer to also stop caring about the arbitrary numeric value, and write code using words that clearly describe what your logic is doing. Imagine trying to figure out what your code did if it were written like so:
    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int qlorbix, flarb, ewi98__, ewI98__, blazzle;
    
    
        blazzle = 17;
        flarb = ewi98__ = ewI98__ = 0;
        while ((qlorbix = getchar()) != EOF) {
            ++ewI98__;
            if (qlorbix == '\n')
                ++flarb;
            if (qlorbix == ' ' || qlorbix == '\n' || qlorbix == '\t')
                blazzle = 17;
            else if (blazzle == 17) {
                blazzle = 42;
                ++ewi98__;
            }
        }
        printf ("%d %d %d\n",flarb, ewi98__, ewI98__);
    
    
        return 0;
    }
    For extra fun with C programs that are hard to read or reason about, look into the IOCCC (Obfuscated C Contest).

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    35
    Thank you! I thought as much,i guess the language and every else are just one big login

Popular pages Recent additions subscribe to a feed