Thread: word count program not working

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    30

    word count program not working

    Hi all,

    I'm trying to create a word count program. With the definition that a word is any sequence of characters that does not contain a blank, tab or newline.

    Every time the program encounters the first character of a new word, it should count a word.

    Here's the code :

    Code:
    #include <stdio.h>
    
    #define IN 1  // inside a word
    #define OUT 0  // outside a word
    
    // count lines, words and characters in input
    int main()
    {
     int c, nl, nw, nc, state;
    
     state = OUT;
     nl = nw = nc = 0; // nl = newline; nw = new word; nc = new character
    
     while ((c = getchar()) != EOF)
     {
      nc = nc + 1;
      if (c == '\n')
      {
    	nl = nl + 1;
      }
      if (c == ' ' || c == '\n' || c == '\t')
      {
    	  state = OUT;
      }
      else if (state == OUT)
      {
       state = IN;
       nw = nw + 1;
      }
     }
     printf ("%d %d %d \n", nl, nw, nc);
    }
    Well...it's not working. I've been typing words but nothing happened. It just won't count...
    Nothing happens, just blank.

    Could someone please point where I did wrong? FYI I'm using a Visual C++ 2008 to compile the program.

    Also, I'm a newbie so please bear with me

    Thank you.
    Last edited by vsovereign; 06-04-2010 at 12:56 PM.

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Works perfectly.

    But you're using your own program wrong. When you're done, you should enter an EOF. In Windows, this is [Ctrl + Z], and follow up with [Enter].

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    30
    Quote Originally Posted by msh View Post
    Works perfectly.

    But you're using your own program wrong. When you're done, you should enter an EOF. In Windows, this is [Ctrl + Z], and follow up with [Enter].
    Thanks! It's working now.

    I'm under the impression that EOF is [Enter] :P

    one more question :

    I have to write :

    abcd efgh [Enter]
    [Ctrl+Z] [Enter]
    to get the result. Why can't I just write :

    abcd efgh [Ctrl+Z] [Enter] ?
    Thanks

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    From the best of my understanding, this is OS-dependent behavior.

    For Windows, [Ctrl + Z] counts as EOF only at the beginning of the line. Ditto for most Linux flavors, except it's [Ctrl + D].

    Try this as input...
    Code:
    [Ctrl + Z]hello world[Enter]
    What you need to understand that you're actually simulating an EOF signal, and what this is depends on the OS, and there is no such thing as an EOF character.

    Maybe someone can explain the technical details better. I'm sure they will be along shortly.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    This is filesystem theory. Basically, the way *most* filesystems store data is to chain together sectors via some sort of linked list. This data structure would contain # bytes/ current record, next segment, etc. The head is normally a bit different with the # total bytes and # total sectors used. Some of the more intelligent filesystems will also allow for multiple files stored in the same sector (IFF space is low).

    The return value would be EOF when then filesystem recognizes that the count of data remaining in the file is 0.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    30
    Quote Originally Posted by Kennedy View Post
    This is filesystem theory. Basically, the way *most* filesystems store data is to chain together sectors via some sort of linked list. This data structure would contain # bytes/ current record, next segment, etc. The head is normally a bit different with the # total bytes and # total sectors used. Some of the more intelligent filesystems will also allow for multiple files stored in the same sector (IFF space is low).

    The return value would be EOF when then filesystem recognizes that the count of data remaining in the file is 0.
    Okay thanks for the explanation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. word counting program
    By mashour06 in forum C Programming
    Replies: 2
    Last Post: 06-09-2009, 03:58 AM
  3. help me out Unrgent wit a cross word program
    By rags in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 11:12 AM