Word Counting

This is a discussion on Word Counting within the C Programming forums, part of the General Programming Boards category; Originally Posted by cookie I also tried to understand it! But since I am a very beggining person, I could ...

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by cookie View Post
    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, char_num, letter_num, blanks;
    	state = OUT;
    	words = lines = tabs = characters = blanks = char_num = letter_num = 0;
    	
    	while((x = getchar()) != EOF)
    	{
    		++ char_num;   /* counts all char's, of any kind */
                    if (x == '\n')
    			++lines;
    		else if (x =='\t')
    			++tabs;
    		else if (x == ' ')
    			++blanks;
                    else
                            ++letter_num;  /* counts just the letters */
    
    		if (x == ' '|| x == '\n'|| x == '\t')   /* no else on this line */
    			state = OUT;
    		else if (state == OUT)
    		{
    			state = IN;
    			++words;
    		}
    	}
    	printf("You have &#37;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);
            printf("You have %d characters\n", char_num);
            printf(" and %d letters.\n", letter_num);
             	*/ }
                    */
    }
    Note the changes in your code.

    How does that look and run?
    Last edited by Adak; 06-17-2007 at 07:16 AM.

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

    Now, it gives me characters for words!

    I fixed the problem where it didn't want to count the words but got into another where it gives me off results at letters! It counts characters, blanks, tabs, lines and all the others right but it counts the letters off!

    Here, I will post the code
    Code:
    #include <stdio.h>
    
    #define OUT 0
    #define IN  1
    
    int main()
    {
    	printf("HELLO! THIS IS A BETTER VERSION OF THE UNIX PROGRAM CALLED wc!\n");
    	printf("THIS PROGRAM TAKES THE ENTER KEY AS A CHARACTER SO EVERYTIME YOU PRESS ENTER IT WILL GIVE YOU A CHARACTER PLUS!"\n);
    	
            while(1)
    	{
    		int x, state, words, lines, tabs, chars, blanks, letters;
    		state = OUT;
    		words = lines = tabs = chars = blanks = letters = 0;
    	
    		while((x = getchar()) != EOF)
    		{
    			++chars;
    			switch(x)
    			{
    			case ' ':
    				++blanks;
    			break;
    			case '\t':
    				++tabs;
    			break;
    			case '\n':
    				++lines;
    			break;
    			
    			}
    			if (x == ' ' || x == '\t' || x == '\n')
    				state = OUT;
    			else if (state == OUT)
    			{
    				state = IN;
    				++words;
    			}
    			else
    				++letters;	
    		}
    		printf("You have &#37;d word(s)\n", words);
    		printf("You have %d letter(s)\n", letters);
    		printf("You have %d character(s) in total:\n", chars);
    		printf("You have %d line(s)\n", lines);
    		printf("You have %d tab(s)\n", tabs);
    		printf("You have %d blank(s)\n", blanks);
    	}
    }
    It gives me like 1, 2, 3 or 4 letters less than I have in the text!
    Last edited by cookie; 06-17-2007 at 12:48 PM. Reason: I solved one of the problems, got into another

  3. #18
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include <stdio.h>
    
    #define OUT 0
    #define IN  1
    
    int main()
    {
    	while(1)
    	{
    		int x, state, words, lines, tabs, chars, blanks, letters;
    		state = OUT;
    		words = lines = tabs = chars = blanks = letters = 0;
    	
    		while((x = getchar()) != EOF)
    		{
    			++chars;
    			if (x == ' ')
    			{
    				++blanks;
    				++words;		
                }
    			else if (x == '\n')
    			{
    				++lines;
    				++words;          }
    			else if (x == '\t')
    				++tabs;
    			else
    				++letters;
    			if (x == ' '|| x == '\n'| x == '\t')
    				state = OUT;
    			else if (state == OUT)
    				state = IN;
    		}
    		
    		printf("You have &#37;d word(s)\n", words);
    		printf("You have %d letter(s)\n", letters);
    		printf("You have %d character(s) in total:\n", chars);
    		printf("You have %d line(s)\n", lines);
    		printf("You have %d tab(s)\n", tabs);
    		printf("You have %d blank(s)\n", blanks);
    	}
    }
    
    /* my output
    You need motivation
    Motivation helps you lean a lot
    You have done it good
    ^Z
    You have 14 word(s)
    You have 60 letter(s)
    You have 74 character(s) in total:
    You have 3 line(s)
    You have 0 tab(s)
    You have 11 blank(s)
    */
    Note the changes
    EDIT: I cant manage a good indendation, because of different editor.


    ssharish2005
    Last edited by ssharish2005; 06-18-2007 at 05:39 AM.

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

    what is the differnece?

    What is the difference betwwen mine and yours? Can you pls highlight what u changed?

Page 2 of 2 FirstFirst 12
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, 06:08 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06: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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21