Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Vote on your favorite hash algorithm here!

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 04-22-2007, 11:11 AM
siren siren is offline
Newbie
 
Join Date: Apr 2007
Posts: 10
Credits: 0
Rep Power: 0
siren is on a distinguished road
Default Need help w/ word count program (ASAP)

I am in need for some assistance....I need to create a program that counts the number of words in certain program. The requirements for the program are
1) The program first reads a text from a txt file.
2)It counts how many times each word appears in the file
3) It displays the word and how many times it appears at the end...

sorry for the bad explanation. Here's an example.

It would call up a txt file such as test.txt
This is a dog.
This is a cat.
The dog loves the cat!!

In this case it needs to count like so
This=1
is=2
a=2
dog.=1
That=1
cat.=1
The=1
dog=1
loves=1
the=1
cat!!=1

Although its an incomplete program, meaning that it counts "dog" and "dog." as two different words, my professor wants it to work out like so....

Another requirement is I combine these two programs I've created before
Code:
#include<stdio.h>
#define SENTENCE_MAX 80
#define LIST_MAX 100

int main(){
  int i,j;
  char ch, sentence[SENTENCE_MAX], Slist[LIST_MAX][SENTENCE_MAX];
  for(i=0;i<LIST_MAX;i++){
    Slist[i][0]=0;
  }
  printf("*------- Sentence List Program -------* \n");
  i=0;
  do{
    ch=getc(stdin);
    j=0;
    while((ch!=10)&&(ch!=EOF)&&(j<SENTENCE_MAX)){
      sentence[j]=ch;
      j++;
      ch=getc(stdin);
    }
    sentence[j]=0;
    strcpy(Slist[i],sentence);
    i++;
  }while(ch!=EOF);
  printf("*-------  Input file  ---------* \n");
  for(j=0;j<i;j++){
    printf("%s \n",Slist[j]);
  }
}
This is a 2d array program

The other program I have to use is
Code:
#include<stdio.h>
#define LIST_MAX 30
int main(){
  int i, CHcount[LIST_MAX];
  char ch, CHlist[LIST_MAX];

  FILE *textin;
  
  for(i=0;i<LIST_MAX;i++){
    CHcount[i]=0;
    CHlist[i]=0;
  }
  
  textin = fopen("test.txt","r");
    do{
    ch=fgetc(textin);
    for(i=0;((???????)&&(???????));i++){
      if(ch==CHlist[i])
	break;}
    
    if(CHcount[i]>0){
      ???????;
    }
    else{
     ?????? ;
     ??????;
    }
  }while((ch!=EOF));
  for(i=0;((???????)&&(???????));i++){ 
    printf("%c = %d \n",CHlist[i],CHcount[i]);
  }
  
  fclose(textin);
}
This program is suppose to count how many times the same letter appears in a text file...However, its still incomplete (the ??????) parts...

I need to combine these two codes in order to create a program that counts how many times a word is repeated....I'm just having a very difficult time becuase everything is already fixed by the professor and not much freedom is given. Any help is appreciated!!!!!!!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-23-2007, 08:14 AM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,578
Last Blog:
CherryPy(thon)
Credits: 55
Rep Power: 28
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



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

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java:Tutorial - Tic-Tac-Toe John Java Tutorials 25 02-27-2008 06:41 PM
Dictonary Program programmer 101 Java Help 9 07-01-2007 01:39 PM
How do I Program another Program? ! bosco General Programming 1 06-15-2007 11:15 AM
Othello program!! 24 hours left! siren C and C++ 1 06-14-2007 11:18 AM


All times are GMT -5. The time now is 08:36 AM.

Contest Stats

Xav ........ 1357.94
MeTh0Dz|Reb0rn ........ 1083.85
WingedPanther ........ 919.18
marwex89 ........ 906.86
morefood2001 ........ 903.18
John ........ 902.37
Brandon W ........ 789.89
chili5 ........ 312.39
Steve.L ........ 264.99
dcs ........ 240.34

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 83%

Ads