Thread: Word Counting Program

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Word Counting Program

    Hello everyone,

    I am new to C programming, but I will do my best to explain my issues. I am currently taking a course and the assignment that we have right now is to create a word counting program. I am suppose to count the characters, lines, and words in any input. The words need to be able to be counted even if there are more than one space inbetween them (i.e. dog cat is 2 words with 4 spaces in between). I am able to make code that counts the words if there is only ONE space, but how can I code it so that it counts with any amount of spaces?

    Below is the current loop that I have that is counting the words.

    Code:
      if (isspace(ch) != 0)
        {
           wordcount++;
        }
    The idea that I have to go about this issue is to somehow make a temporary variable that reads in the spaces, and when there is a space, it increments the space count. When it reaches a character, it starts to count for a new word. Once it reaches another space, the word count increments. The issue is that I am not sure how I can put this idea into code. Can someone help please?

  2. #2
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    As a side note, I understand that this problem would best be done through arrays and strings, but we have not gone over arrays and strings in our class yet, so I do not want to use them in this assignment.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Use a variable to keep track of whether or not the previous character was a space. If it was, don't count it as a new word, otherwise increment wordcount.

    Something like:
    Code:
    int previousCharWasSpace = 0;
    if(isspace(ch))
    {
      if(!previousCharWasSpace)
        wordcount++;
    }
    else if(previousCharWasSpace)
      previousCharWasSpace = 0;
    Last edited by itsme86; 02-23-2011 at 02:40 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    I implemented that into the code, but it still only works when there is only one space.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, PSV!

    You need a "state" switch, to indicate if the char you are currently checiking, is inside a word, or is it outside a word.

    If it's inside, then count the char, etc. If it's outside the word, no matter how many spaces you run across, you're still outside the word, so count nothing except spaces, etc.

    Your "switch" (think of a light switch, on or off), will be an int variable (later, a boolean one, perhaps). It turns on when you get to the first letter or digit, of the input, and turns off when you (boy I feel tired), when you (um um, I need some zzz's), when the program is checking a . . . char. ZZZzzz


    What turns your inWord variable from ON to OFF (1 to 0)?

    if(inWord...//etc.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by psv1120 View Post
    I implemented that into the code, but it still only works when there is only one space.
    I left a bug in there for you to find!
    Code:
    int previousCharWasSpace = 0;
    if(isspace(ch))
    {
      if(!previousCharWasSpace)
        wordcount++;
      // Something should go right here
    }
    else if(previousCharWasSpace)
      previousCharWasSpace = 0;
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    What is happening is that it is adding a word whenever there is more than one space. Here is the updated code with your suggestion:

    Code:
    int prevspace=0; //check if previous character was space
        if (isspace(ch) != 0)
        {           
           if (!prevspace)
           {
             wordcount++;
             prevspace = 0;
           }
           else if(prevspace)
           {
             prevspace = 0;
           }
        }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you're going to "inch worm" logic, then you need to set previous space to the value of the current space, before you loop again.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Not quite.
    Code:
    int prevspace = 0;
    if(isspace(ch))
    {
      if(!prevspace)
        wordcount++;
      else
        prevspace = 1;
    }
    else if(prevspace)
      prevspace = 0;
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    hm. for some reason I am not getting the correct output...even with that fix...

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That's because I did it wrong. Oops. This should work:
    Code:
        if(isspace(ch))
        {
          if(!prevspace)
          {
            wordcount++;
            prevspace = 1;
          }
        }
        else if(prevspace)
          prevspace = 0;
    Don't forget to add 1 to wordcount after the loop if prevspace is 0. Otherwise the last word won't get counted (unless the input ends with a space).
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    Thanks for your help! I appreciate it greatly!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 01:40 PM
  2. Replies: 2
    Last Post: 07-28-2010, 02:07 PM
  3. Replies: 2
    Last Post: 12-02-2007, 05:40 AM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  5. Relocation in .obj -files
    By willkoh in forum C++ Programming
    Replies: 6
    Last Post: 04-06-2005, 01:59 PM