+ Reply to Thread
Results 1 to 7 of 7

Thread: New to Posting: Help with word comparison in C

  1. #1
    Newbie jaano31 is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    2

    New to Posting: Help with word comparison in C

    so I'm trying to write a program that reads in a text file and creates a histogram of all the words that are present within that text (how many times each word appears in the sentence). I'm basically supposed to use a two dimensional array (where the first element contains the word and the second element contains the number of occurrences of of that word). the output should look like this:

    this: * 10%
    is: ***** 50%
    word: ** 20%
    hello: *** 30%

    I'm not sure even how to begin as I don't know how to separate words from a text file (search for white spaces?). And I don't have enough experience working with character functions to know shortcut functions to analyze the words.. I would really appreciate any piece of advice on this.. thanks so much

  2. #2
    Learning Programmer veda87 is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    69

    Re: New to Posting: Help with word comparison in C

    Let me give you my way of solving this program...

    Since the number of words in the file is not known, static array is not possible.. So a dynamic array is needed. So I would go for Linked list. For example,

    Code:
    struct array {
    char *text;
    int occurance;
    struct array *next;
    };
    The words are to be separated by white-spaces, full-stop, comma, end of line and end of file... So these words in a temporary variable (say char *tempWord)

    As soon as the word is found do these following steps:
    1. Find the next word
    2. check whether the list is free
    2.a. If yes, create an entry, add the word to the entry, increment the occurance to 1 and make the next to point NULL.
    Now search for the word in the entire file using strstr ( strstr is the command that points to the first occurrence in word of any of the entire sequence of characters specified in file, or a null pointer if the sequence is not present in file. )
    when the word is found, increment the occurance and move the file pointer to next...
    do this till EOF is reached. then go to step 1 if EOF is reached.
    2.b. If no, search for the word in the list
    2.b.(i). if present in the list, then go to step 1.
    2.b.(ii). if not present, follow the step 2.a and do update the list.


    follow these steps till EOF is reached...
    ----------------------

    this is an idea.. you have to use variables accordingly..

  3. #3
    Programmer manux is on a distinguished road
    Join Date
    Oct 2008
    Age
    16
    Posts
    112

    Re: New to Posting: Help with word comparison in C

    I agree with linked lists, but in case you dont understand the concept, you also might want to allocate dynamically a new "page" each time you find x new words.
    What I mean is that you start with an empty char* array[32], which you extend when you find the 33rd word(extend it another 32 char*s).

    As for finding words, you simply need to keep two pointers around:
    -1st the pointer to the last end of a word
    -2nd the pointer to the end of the present word.
    You need to increment the 2nd one until you find a word separator(space, dot comma).

    If you want to compare two strings, you can use strcmp(I think in stdlib.h, or string.h), or you could easily write your own function(which I'd recommend if you're in a learning context)

  4. #4
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,640
    Blog Entries
    57

    Re: New to Posting: Help with word comparison in C

    The first thing I would suggest is making sure you have a list of useful functions: strcmp() comes to mind. I'm guessing this is homework, so you can probably assume words won't be overly long: 30 characters should be good, but be sure to test for it. Also, make sure you understand how scanf() interprets a word. If you can make scanf() do most of the work, that will help a LOT.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #5
    Newbie jaano31 is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    2

    Re: New to Posting: Help with word comparison in C

    Thanks for the responses!.. the professor doesn't want us to delve too deep into pointers for this. He claims it can be done without. I have been able to read in the number of words in the text, and want to store each word in a row of a two-dimensional array (i was hoping to then loop through each row and use strcmp() to compare) but I can't put the array together. I'm looking at scanf and sscanf but am confused as to how they work to separate words.
    Thanks for all the responses so far..

  6. #6
    Code Warrior Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest's Avatar
    Join Date
    Sep 2009
    Location
    United States
    Age
    16
    Posts
    2,589
    Blog Entries
    2

    Re: New to Posting: Help with word comparison in C

    Take this small example:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char word[50];
    	scanf("%s", word);
    	printf("%s", word);
    	return 0;
    }
    Compile that program and run it. Type in two words and press enter. The program should only output one word. scanf stops putting data into the word variable when it hits a newline or a space so that should be convenient to get separate words. The only problem is that scanf will include things like periods and commas, so you will have to filter those out.
    Vista users shouldn't have sigbars.
    < Holy cow! Two images!

  7. #7
    Moderator ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon is a name known to all ZekeDragon's Avatar
    Join Date
    Jul 2009
    Location
    Nowhere, Washington
    Posts
    1,758
    Blog Entries
    39

    Re: New to Posting: Help with word comparison in C

    I disagree with Guest. scanf() has the same problem gets() does, in that there's no way to tell scanf() what the buffer size is, which will very easily lead to buffer overflows. You need to use something like fgets() to collect it onto a buffer, check to ensure the word isn't too large, then you can sscanf that line and do any necessary strncmp()s, however I'd still use a custom function. So, yeah, that's not the only problem with scanf.
    Should I get a userbar here?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Java: Word Counter
    By chili5 in forum Classes and Code Snippets
    Replies: 2
    Last Post: 09-02-2009, 06:55 AM
  2. Guess The Word.
    By Paradox in forum Java Tutorials
    Replies: 5
    Last Post: 01-16-2009, 09:48 AM
  3. Remove a word from sentence using arrays
    By Hawk1 in forum C and C++
    Replies: 4
    Last Post: 12-09-2008, 02:20 AM
  4. Getting ActiveX Control To Send to AbiWord not MSFT Word
    By dranfu in forum Visual Basic Programming
    Replies: 6
    Last Post: 10-13-2008, 09:39 PM
  5. Dictonary Program
    By programmer 101 in forum Java Help
    Replies: 9
    Last Post: 07-01-2007, 01:39 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts