5 Replies - 2660 Views - Last Post: 24 April 2007 - 01:04 PM Rate Topic: -----

#1 siren128  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 13-April 07

Need help w/ word count program (ASAP)!

Posted 22 April 2007 - 09:12 AM

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
#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
#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!!!!!!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Need help w/ word count program (ASAP)!

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Need help w/ word count program (ASAP)!

Posted 22 April 2007 - 09:42 AM

Your teacher claims that just replaceing the ?????'s with code will solve the problem?
Was This Post Helpful? 0
  • +
  • -

#3 siren128  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 13-April 07

Re: Need help w/ word count program (ASAP)!

Posted 22 April 2007 - 10:10 PM

Hey NickDMax..Thanks for looking at this thing....My teacher is claiming that by filling in the ????? in the second program, that will get me a program that counts the number of times a letter frequently appears in a .txt file...

He wants us to combine the first 2d array program and the completed letter counting program to create a word counting program....

He's also saying that by completing the ????? parts, the second letter counting program is suppose to work flawlessly...Thanks for the help b4 hand.
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Need help w/ word count program (ASAP)!

Posted 23 April 2007 - 10:55 AM

ok, filling the correct expressions will count letters wonderfully. (it would not count the words). If I were you I would go line by line and comment them. Understand what each one is doing.

To give you a hint: The CHlist[] array hold all of the characters that we have run into so far. So the initial loop needs two conditions, one is that it does not go to high (LIST_MAX) and the other is that CHlist[i] must not be 0, if it gets to apoint where CHlist[i]==0 then we found a new character.

That is what goes in the next part. if CHcount[i]==0 then we need to make it =1, and we need to add ch to CHlist.
Was This Post Helpful? 0
  • +
  • -

#5 siren128  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 13-April 07

Re: Need help w/ word count program (ASAP)!

Posted 24 April 2007 - 04:44 AM

NickDMax, I really need your help! I beg you! Can I please see just that part of the code?
I worked on it for a while, but it just isn't working...The deadline for the assignment is coming up, and I really can't move onto the next part without this/ I followed your advice and hints, but I just can't figure it out..

So far all I've been able to do was
#include<stdio.h>
#define LIST_MAX 300
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;((CHcount[i]<LIST_MAX)&&(CHlist[i]!=0));i++){
	  if(ch==CHlist[i])
	break;}
	
	if(CHcount[i]>0){
	  CHcount[i]=CHcount[i]+1;
	}
	else{
	  CHlist[i]=ch;
	  CHcount[i]=1;
	}
  }while((ch!=EOF));
  for(i=0;((CHcount[i]<LIST_MAX)&&(CHlist[i]!=0));i++){ 
	printf("%c = %d \n",CHlist[i],CHcount[i]);
  }
  
  fclose(textin);
}



PLEASE!!! HELP ME OUT WITH JUST THIS PART!!
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Need help w/ word count program (ASAP)!

Posted 24 April 2007 - 01:04 PM

Almost right, try this:
#include<stdio.h>
#define LIST_MAX 300
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<LIST_MAX)&&(CHlist[i]!=0));i++){
	  if(ch==CHlist[i])
	break;}
	
	if(CHcount[i]>0){
	  CHcount[i]++;
	}
	else{
	  CHlist[i]=ch;
	  CHcount[i]=1;
	}
  }while((ch!=EOF));
  for(i=0;((i<LIST_MAX)&&(CHlist[i]!=0));i++){
	printf("%c = %d \n",CHlist[i],CHcount[i]);
  }
  
  fclose(textin);
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1