Thread: Word Counter

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    57

    Lightbulb Word Counter

    Can someone assist me in writing a function that counts words that the user inputs?

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    Use easier terms I'm a newbie.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    I have the StrSpn() function written. But I need this to not only count but output the words individually. I'm still working. Any hints would be appreciated.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    I cant use anything but stdio.h
    This is only part of the assignment but here what i have for this part:

    Code:
    #include <stdio.h>
    
    #define IN 1
    #define OUT 0
    
    main()
    {
      int c;
      int nw;
      int state;
    
      state = OUT;
      nw = 0;
    
      while((c == getchar()) != EOF)
      {
        if(c == ' ' || c == '\n' || c == '\t')
          state = OUT;
        else if(state == OUT)
        {
          state = IN;
          ++nw;
        }
      }
    
      printf("%d", nw);
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>while((c == getchar()) != EOF)
    This is wrong, it should be
    >>while((c = getchar()) != EOF)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    57
    Ok.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM