View Single Post
  #1 (permalink)  
Old 04-22-2007, 12:11 PM
siren siren is offline
Newbie
 
Join Date: Apr 2007
Posts: 10
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!!!!!!!!
Reply With Quote

Sponsored Links