Thread: Word occurrence logic

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    21

    Word occurrence logic

    I am trying to count the occurences of a words in a sentence. For example in the sentence To be, or not to be: The occurence of "to" would be 2, "be" would be 2, "or" would be 1. I just need a point in the right direction as far as logic goes. Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the first step is to extract words, and print them out just to show that you can do that successfully.
    Then you add the words to some kind of list (if they're not in the list), or increment a count if they are in the list.
    Code:
    struct foo {
        char word[20];
        int count;
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    You might want to look into string tokenizers.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    21
    Thanks guys I will look into it.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I would use a map of string to int to hold the strings and their counts. Also, operator >> on strings separates words based on whitespace, but you'll have to do extra work to remove punctuation like commas or periods.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 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