Word occurrence logic

This is a discussion on Word occurrence logic within the C++ Programming forums, part of the General Programming Boards category; I am trying to count the occurences of a words in a sentence. For example in the sentence To be, ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    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,317
    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, 05:44 PM

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