Thread: Word Counting

  1. #1
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Word Counting

    I am trying to understand a program that counts words! It has an explanation in the book, but it is lame! I do not understand why they have so many variables and why do they need the OUT and IN symbolic constants! This program is confusing me so much and I can't go on further with the book if I do not understand how this program works!
    Code:
    #include <stdio.h>
    #define IN    1
    #define OUT 0
    
    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("&#37;d %d %d\n", nl, nw, nc);
    }
    Maybe I have few syntax errors in here, but do not consider them as I already fixed some that I know and it works ! My problem is I donot understand HOW this program works and why is it made so difficult, couldn't it be written in a better way??
    Last edited by cookie; 06-15-2007 at 08:49 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The IN and OUT variables are tracking the two states that you have when scanning through text: the cursor or current position, is either INside a word, or it is OUTside a word.

    It's important sometimes to track exactly which state a program finds itself in, as it goes. If you look at each character in a simple text message, and follow the program's logic, I believe it will hit you suddenly "AH!" that's what's going on here.

    Sort of like Algebra, or Geometry or whatever. It's pretty dark at first, but then "Ah!" The light goes on, and you can SEE!

    I would call it a good program to learn from. It's not about being "better" - it's about being CLEAR and showing good logic.
    Last edited by Adak; 06-15-2007 at 09:04 PM.

  3. #3
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Last Part Most Confusing

    Why does it have this in the else if body?

    Code:
    else if (state == OUT)
        state = IN
        ++nw;
    what does this do?? if state is outside of word
    state is inside
    increment nw


    So if u are outside of a word, make it be inside of a word, and increment nw!

    The part that i do not understand at all is this one
    else if (state == OUT)
    state = IN;
    I do understand ++nw;
    Last edited by cookie; 06-15-2007 at 09:41 PM. Reason: Found out something during this time

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Look at the whole block of code:

    Code:
    if(c == ' ' || c == '\n' || c == '\t')
                state = OUT;
            else if (state == OUT) {
                state = IN;
                ++nw;
            }
    Now imagine that you're scanning thru some text, and you are IN side a word. Now you come to a blank space or a newline, or a tab.

    That would switch you to being OUT side a word. Now if you are OUT side a word, and you came to a char, and that char was NOT a blank space OR a newline, OR a tab, then the program goes to that else if, and finds out "if I have reached a new char, and that new char is not any of those three mentioned choices above, then it MUST be a char to start a new word. Therefore, I'm back IN side a word again. So increment the new word variable by one.

  5. #5
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Thanks! I did understand it

    I understood the whole program and I want to thank anybody! I also decided to challenge myself by writing a program that counts words only on myself! I know how to make it cound lines, tabs, backslashes, etc!BTW, how do I make a backspace?? I have the sequence \b put in the right spot! But what is a backspace in a C program! I pressed the backspace button but still, nothing!

    Here is the code for the WORD COUNTING program, not the one with backspace problems!
    Code:
    #include <stdio.h>
    
    #define OUT 0
    #define IN  1
    
    int main()
    {
    	int x, state, nw;
    	
    	state = OUT;
    	nw = 0;
    	while((x = getchar()) != EOF)
    	{
    	    if (x == ' '|| x == '\n'|| x == '\t')
    	    	state = OUT;
    	    
    	    else if (state == OUT)
    		{
    			state = IN;
    			++nw;
    		}
    	}
    	printf("You have %d words\n", nw);
    }
    If you have time, pls consider giving me some critics and commentarries on it and help me improve it!

    Thanks agin, To Everybody

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What about

    ... :? (: /* --- */


    They all counted as words?

    You cannot see backspace in the file because pressing backspace just removes the last char from the output buffer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    One thing I will say about this topic and your posts in general is that if you're doing this for your own learning and this has nothing to do with homework or programming courses, then your motivation level appears to be really high and as such is going to really count for something in your learning process.

    Doing the exercises in the book, and inventing your own.... Keep at it, and I see no reason why you won't get really good with this.

  8. #8
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Yes, I am doing this just because I want to learn C!

    I did want to take some courses but I thought again and I think it is more rewardable(at least for me) to learn C on my own! I am not doing it for money, just pure pleasure and satisfaction!

    And one more thing I am going to bother you people with is that I want to make a program that will read input and return output one word per line!

    I thought I could do printf("%d\n\n", word)but that would only leave 2 lines! Give me an idea!I really don't know !


    Thanks!

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Do you mean, you want to write a program which will read some lines from the user, but print only the first word of each line? That's quite simple; you just have to find the first whitespace character (or the first !isalnum() character) and print up until it.

    Or do you mean, you want to write a program which will read some lines from the user, printing each word in each line entered on a separate line? That's a little tougher. You'd have to keep track of the beginning of each word, find the end of the word, etc, until you used up all of the string. You could use strtok() for this if you wanted to.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    Well You can tell me anything u want, I barely know how to progrm!

    I am trying to print one word per line from input!

    So I suck at c would be:

    I
    Suck
    At
    C


    That is what I am trying to do!

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Right, that's what I thought. Well, you could use strtok(), or something like this:
    Code:
    int start = 0, pos;
    do {
        start = skip_word(str, start);
        pos = skip_space(str, start);
        print_word(start, pos);
        start = pos;
    } while(start != str_len);
    I would implement skip_space() like so:
    Code:
    int skip_space(const char *str, int start) {
        while(isspace(str[start])) start ++;
    
        return start;
    }
    isspace() is in <ctype.h>. It's very useful.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    As I said before, I just started

    I know I am very very annoying but I just started learning C! The book gives this exercise in a section of word counting! As far as I got with this book, it never said one thing about ctype.h or isspace() or any functions like those! I just know how to count lines, words, spaces, tabs, copy input to output, and very simple stuff! What I am trying to say is that there has to be a more simple way to do this program! They wouldn't be stupid to give this exercise in the book when it hasn't covered this stuff yet!

    So please can you tell me a simpler way of doing this program, even if it is longer! Just a very noob way to take input with getchar() or scanf() and print the output one word on a line!

    Thanks!

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    When you're in state IN, and you set state to OUT, print a '\n' char. Otherwise when you're in state IN, but you don't set state to OUT, print a char.

    Try something like that.

    BTW, we all know you're new. Don't beat yourself over the head here as "annoying" and a "noob". You're doing fine. Just ask clear and concise questions. When you're stuck on something, show your code, explain what you're thinking, what you expect it to do in great detail, and what it's doing instead.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One thing to keep in mind with programming in C - there's always more than one way to get the job done. Frequently, there are several ways.

    You have just worked on a program to count words, and you know it works, right?

    So I'm thinking of a simple program like this:

    Code:
    char input[80] = '\0';   /* this is NULL in char talk */
    
    while (input != 'q')   {
       puts(" Enter some words, please: ");
       fgets(stdin, 79, input)
       
       printf(" %s \n ", input);
    
    /* NOW, right here, you know what the string input is, and you know it has been entered OK,
    
    Now we'll add some logic from your word counting code here, but change it a bit. Instead of *counting* the 
    words, you want it to print just that one word, and then also print out a newline char '\n', 
    when the end of the word is reached.
       printf("\n\n");
       length = strlen(input);
       for (i = 0, i < length; i++)  {
          if (input[i] != ' ' && input != '\n')
             printf("%c", input[i]);
          else
             printf("'\n'");
       }
    }
    Hope that helps. Don't get too frustrated because the initial learning cure is steep. Just go with it, and try not to get frustrated. C is not the easiest language around. It was written by
    brilliant workers at Bell Labs, which was one of our better research labs, and it was based on
    the work of still other brilliant researchers, who had invented langauges like 'B', before them.


    As such, C was designed for professional programmers, with no intent to make it super duper easy for beginners. *POWERFUL* (efficient), was the name of the game.

    Don't worry if the syntax for the code above won't compile, either. That's just a suggestion,
    off the cuff. Play with it, see what does and does not work with it, and make improvements
    and changes to it, as you see fit. Experimenting is important.


    If you get stuck, just post up your problem.

  15. #15
    #C-help
    Join Date
    Jun 2007
    Location
    Las Vegas
    Posts
    53

    I tried your code! It did nto compile, I also messed a bit with it!

    I also tried to understand it! But since I am a very beggining person, I could not understand much of it! One day passed, I wrote no code! I felt like I forgot anything I learned up to that point so I decided to write a program that includes all the others I've done! But the program doesn't work!

    The problem with this is that it keeps telling me I have 1 word even if I write whole pages! One word if I type anything, 0 words if I type nothing!

    As you see, I have an integer called characters! That was supposed to count the characters in my text! I forgot that also!

    Here is the code, it is driving me crazy and it is on my nerves since I can't figure out the problem why it tells me I have 1 word when I have many!

    I would also appreciate some help on character counting

    I also posted this on another thread, my mistake as it is 3:23AM when I am writing this!
    Code:
    #include <stdio.h>
    
    #define OUT 0
    #define IN  1
    
    int main()
    {
    	while(1)
    	{
    		int x, state, words, lines, tabs, characters, blanks;
    		state = OUT;
    		words = lines = tabs = characters = blanks = 0;
    	
    		while((x = getchar()) != EOF)
    		{
    			if (x == '\n')
    				++lines;
    			else if (x =='\t')
    				++tabs;
    			else if (x == ' ')
    				++blanks;
    			else if (x == ' '|| x == '\n'|| x == '\t')
    				state = OUT;
    			else if (state == OUT)
    			{
    				state = IN;
    				++words;
    			}
    		}
    		printf("You have %d words:\n", words);
    		printf("You have %d lines:\n", lines);
    		printf("You have %d tabs :\n", tabs);
    		printf("You have %d blanks:\n", blanks);
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String - word counting!
    By shardin in forum C Programming
    Replies: 2
    Last Post: 07-11-2007, 05:08 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  4. Word Counting
    By Achillles in forum C++ Programming
    Replies: 9
    Last Post: 09-11-2002, 02:09 PM
  5. follow up with char I/O and line, word, and char counting
    By Led Zeppelin in forum C Programming
    Replies: 1
    Last Post: 03-23-2002, 09:26 PM