Go Back   CodeCall Programming Forum > Software Development > C and C++
Register Blogs Search Today's Posts Mark Forums Read

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-12-2009, 11:51 PM
Newbie
 
Join Date: Oct 2009
Posts: 2
jaano31 is an unknown quantity at this point
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 10-13-2009, 02:06 AM
Learning Programmer
 
Join Date: Aug 2009
Posts: 65
veda87 is an unknown quantity at this point
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..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-13-2009, 10:15 AM
Programmer
 
Join Date: Oct 2008
Age: 16
Posts: 112
manux is on a distinguished road
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-13-2009, 10:57 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-13-2009, 03:58 PM
Newbie
 
Join Date: Oct 2009
Posts: 2
jaano31 is an unknown quantity at this point
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..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 10-13-2009, 11:29 PM
Guest's Avatar
Code Warrior
 
Join Date: Sep 2009
Location: United States
Age: 16
Posts: 2,479
Guest is a name known to allGuest is a name known to allGuest is a name known to allGuest is a name known to allGuest is a name known to allGuest is a name known to all
Send a message via AIM to Guest Send a message via MSN to Guest Send a message via Yahoo to Guest
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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 10-14-2009, 01:50 AM
ZekeDragon's Avatar
Code Warrior
 
Join Date: Jul 2009
Location: Nowhere, Washington
Posts: 1,721
ZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to allZekeDragon is a name known to all
Send a message via AIM to ZekeDragon Send a message via MSN to ZekeDragon Send a message via Skype™ to ZekeDragon
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.
__________________
On Hiatus...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
char, compare, strings, text file, words



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


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


All times are GMT -5. The time now is 11:00 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0