Thread: word counting program

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    11

    word counting program

    I have an exercise says:
    Write a program that reads input as a stream of characters until encountering EOF. Have it report the average number of letters per word. Don't count whitespace as being letters in a word. Actually, punctuation shouldn't be counted either, but don't worry about that now. (If you do want to worry about it, consider using the ispunct() function from the ctype.h family.)

    I wrote this code for the solution:
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <ctype.h>
    
    int main(void)
    {
    	int c;
    	bool inword = false;
    	int w_count = 0, c_count = 0;
    	
    	while ((c=getchar())!= EOF)
    	{
    		if (isspace(c) && !inword)
    			continue;
    		if(!isspace(c) && !inword)
    		{	
    			inword = true;
    			w_count++;
    		}
    		
    		if(isspace(c) && inword)
    			inword = false;
    		if(!isspace(c))
    			c_count++;
    	}
    	printf("chars = %d\nwords = %d\n", c_count, w_count);
    	printf("avg num of letters per word = %d\n", c_count/w_count);
    	
    	return 0;
    }
    is it right (I didn't care about punctuation)? if something wrong, could you explain why?
    Thank you very much.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The only issue I could see is that of design.

    For one, you don't need the first check along with the continue aspect of the loop. Since you are not using any else clauses, though, each of the other if statements will be evaluated after that, which is probably not what you want. It's not necessarily mission critical, but it might be a slight waste of execution time. Technically it is still correct in the sense that it may finish the task properly.

    With that said, if you attempt to change it, you will then introduce (or discover) a bug, namely this part:

    Code:
    if(!isspace(c) && !inword)
    {	
    	inword = true;
    	w_count++;
    }
    There is no count incremented for the character count, which you did take care of later on (so it should work now), but would be broken if you introduced "else if" into the code.

    Plus, there is some redundant checking, as I started to get at earlier. In short, you might be able to rewrite your loop as follows (Please note, I didn't test this, nor did I take too much time looking at it.):

    Code:
    while ((c=getchar())!= EOF)
    {
    	if(!isspace(c))
    	{	
    		if(!inword)
    		{
    			inword = true;
    			w_count++;
    		}
    		c_count++;
    	}
    	else if(inword) inword = false;
    }
    As I said, this isn't entirely mission critical. It's just a design thing. You appear to understand what you're doing.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Whether your program is right or not, is not up to us - it's up to you and your instructor.

    Just as important as being able to program, is the ability to at least (at the very least), show that your program is either giving correct answers, or gives incorrect answers.

    Any discussion of run-time, etc., is secondary. If your program isn't accurate, it doesn't matter how fast it runs, how small the program is, or how easy to maintain it may be.

    You can count words in various phrases to test your program, just as well as anybody else, so get to it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Replies: 2
    Last Post: 12-02-2007, 05:40 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. Relocation in .obj -files
    By willkoh in forum C++ Programming
    Replies: 6
    Last Post: 04-06-2005, 01:59 PM