View Single Post
  #2 (permalink)  
Old 04-23-2007, 09:14 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,651
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

I would do it a little different.
I know you is required to use the two programs you've done before, but I will show you another slightly different solution to your problem, mostly because I will not do your homework. I've made a program, you can use it to look up how to do it, take snippets, or whatever you want.
Code:
/* 
	This is the libraries we're using...
	
	stdio:
		printf
		getc
		fopen
		fclose
		
	string:
		memset
		strcmp
		strcpy
*/
#include <stdio.h>
#include <string.h>

/* Some defines used by the application */
#define FILE_NAME		"test.txt"
#define WORDS_AMMOUNT	100
#define WORDS_LENGTH	50

/* 
	This is the structure, which holds the words 
	
	_word_ is the array of characters that holds
	each string.
	_ammount_ holds the ammount of occurrences of
	the string.
*/
typedef struct
{
	char word[WORDS_LENGTH];
	int  ammount;
} WORDS;

/* Fills an array of characters with NUL (\0) */
void clearString(char *string)
{
	memset(string, '\0', sizeof(string));
}

/* Initializes the list of words */
void initialize(WORDS *list, int size)
{
	int x;
	for(x = 0; x < size; x++)
	{
		clearString(list[x].word);
		list[x].ammount = 1;
	}
}

/* 
	Check if the specific string (needle) is in the
	array (haystack) - the array is a fixed size (ammount).
*/
int inArray(WORDS *haystack, char *needle, int ammount)
{
	int i;
	for(i = 0; i < ammount; i++)
		if(strcmp(haystack[i].word, needle) == 0) /* We found it, return index-position */
			return i;
	return -1; /* We didn't find anything, return not-true */
}

int main()
{
	FILE  *fileHandle;
	WORDS  wordList[WORDS_AMMOUNT];
	char   string[WORDS_LENGTH];
	int    stringCount  = 0;
	int    listCount    = 0;
	int    listIndex, i;
	
	/* Initialize list and clear string */
	initialize(wordList, WORDS_AMMOUNT);
	clearString(string);
	
	/* Create file stream */
	fileHandle = fopen(FILE_NAME, "r");
	do
	{
		/* We get one character from the filestream into the string */
		string[stringCount] = getc(fileHandle);
		
		/* Check if the counter is at a space or newline, ... */
		if(string[stringCount] == 32 || string[stringCount] == 10)
		{
			/* ... if it is, clear that character. */
			string[stringCount] = '\0';
			
			/* Then check if the words already is in the list */
			if((listIndex = inArray(wordList, string, listCount)) != -1)
				wordList[listIndex].ammount++; /* if it is, increase the occurrence of the word... */
			else
			{
				/* else insert the word and increase the counter of the list */
				strcpy(wordList[listCount].word, string);
				listCount++;
			}
			
			/* When we are finished using the string we're clearing it, and set the counter to zero */
			clearString(string);
			stringCount = 0;
		}
		else
			stringCount++;
	} while(string[stringCount-1] != EOF);
	
	/* Close the file stream */
	fclose(fileHandle);
	
	/* Print the result to the user */
	for(i = 0; i < listCount; i++)
	{
		printf("WORD #%d\n", i);
		printf(" - Word:          %s\n", wordList[i].word);
		printf(" - Times occured: %d\n", wordList[i].ammount);
		printf("\n");
	}

	return 0;
}
I've commented the code, so I'll not say anymore about it.
Reply With Quote