Word Counter

This is a discussion on Word Counter within the C++ Programming forums, part of the General Programming Boards category; Hi all, I need some help figuring out a way to count words in C++. More specifically I need help ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Question Word Counter

    Hi all,
    I need some help figuring out a way to count words in C++. More specifically I need help in three issues. Let me back up a bit, the program that I have to do takes the keystrokes that I enter and it counts the number of lines, words and characters. Now counting words, believe it or not was the easy part for me. I just can't count the number of lines or the number of characters and its frustrating because all its giving me is garbage. Here's the code that I have so far:
    Code:
    #include <iostream.h>                                                                                                                         
    #include <string.h>                                                                                                                           
                                                                                                                                                  
    
                                                                                                                                                  
    int charactercount, linecount, wordcount;                                                                                                                                    
                                                                                                                                                  
    void printCharacters( const char * );                                                                                                         
                                                                                                                                                  
    int main() {                                                                                                                                  
      char blurb[150];                                                                                                                            
      char *tokenPtr;   //pointer used to count words                                                                                             
      char *charPtr;    //pointer used to count characters                                                                                        
      //create a dynamic array that counts each character put into it                                                                             
      cin.getline( blurb, 100, '\n');                                                                                                             
                                                                                                                                                  
      //The following four lines will count the number of words used.                                                                             
      tokenPtr = strtok( blurb, " " );                                                                                                            
      for (wordcount = 0; tokenPtr != NULL; wordcount++ ){                                                                                        
        tokenPtr = strtok( NULL, " " );                                                                                                           
      }                                                                                                                                           
      printCharacters( blurb );                                                                                                                   
      //The following lines will count the number of characters used.                                                                             
      for ( charcount = 0; *charPtr != '\0'; charPtr++){                                                                                          
        cout << "charPtr: " << *charPtr;                                                                                                          
      }                                                                                                                                           
                                                                                                                                                  
      cout << "line : " << linecount << ", word : " << wordcount                                                                                  
           << ", char : " << charactercount << "\n";                                                                                                   
                                                                                                                
    }
    [code tags added by ygfperson]
    Oh, and one more thing the array that I am supposed to be reading from does not have any previously allocated space since once the user executes the program all input is being taken into the keyboard until you hit the <Enter> button. How would I go about getting enough space in the array for any length of string(s) that the user types in before I manipulate his string? Thank you in advance for all those people that are helping me out.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>tokenPtr = strtok(blurb, " ");
    You do realise that using strtok() helps to destroy the string? In your code, you are changing all the spaces to nulls, so how do you expect to be able to reuse the string?

    Number of characters: strlen() maybe.
    Number of words: count the spaces, maybe using strchr() in a similar fashion to how you used strtok().
    Number of lines: How many times are you calling getline()?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Smile One more question...

    Hey again all,
    I have one more question with my word counter algorithm. Like I said earlier, when I execute the program, I should be able to write on the screen exactly what my string of words are going to be. The user is also allowed to span multiple lines when they push the <Enter> key. Is there a way to NOT let the computer taken control again until after the user hits a certain escape sequence? I have the getline() function in there but that would only do it for one line... the user can span as many lines as he wants to... here's my code so far:
    (P.S. - Thank you for your help Hammer, your awesome)
    Code:
    #include <iostream.h>                                                       
    #include <string.h>                                                         
                                                                                
                                                                                
    int charcount, linecount, wordcount;                                                                        
                                                                                            
                                                                                
    int main() {                                                                
      char blurb[150];                                                          
      char *tokenPtr;   //pointer used to count words                           
      char *charPtr;    //pointer used to count characters                      
                                                                                
      cin.getline( blurb, 150, '\n');                                           
                                                                                
      //The following lines will count the number of characters used.           
      tokenPtr = blurb;                       
      //The following lines will count the number of words used.                
      tokenPtr = strtok(blurb, " ");                                            
      for (wordcount = 0;tokenPtr != NULL; wordcount++)                         
        {                                                                       
          tokenPtr = strtok(NULL, " ");                                         
        }                                                                       
                                                                                
                                                                                
                                                                                
      cout << "line : " << linecount << ", word : " << wordcount                
           << ", char : " << charcount << "\n";

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: One more question...

    >>Is there a way to NOT let the computer taken control again until after the user hits a certain escape sequence?
    Yes, it's called EOF, which one of CTRL+D or CTRL+Z in most cases.
    Code:
    while ( cin.getline(string, maxLength) && !cin.eof() ) 
    {
        /* Do something */
    }
    In this example, we still process line by line though. There are probably other ways too...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    This is what i would do for the thing:
    Code:
    char input;
    int lines;
    int words;
    int characters;
    while((input = getche()) != /*signal for quit*/)
    {
         ++characters;
    
         if(input == ' ')
              ++words;
         else if(input == '\n')
              ++lines;
    }
    I haven't tried it, but it might work...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

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, 05: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

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