F
Foodbank
Hello,
I'm trying to develop a program that will enable me to count the number
of words in a text file. As a plus, I'd like to be able to count how
many different words there are too. I have a decent start on the
program, but am quite unsure of where to move from here. I need to
malloc space for the array, but am not sure how to. Also, I believe
that strlen may come into play. Although I've browsed similar topics,
I'm also still unsure of the loop formatting to actually count the
words.
Any help would be greatly appreciated.
Thanks,
James
#include <stdio.h>
#include <stdlib.h>
#define MAXWORDS 4000 //less than 4000 total words in the
//text file
char *word[MAXWORDS];
int wordcount[MAXWORDS];
#define MAXWLEN 30 //no words larger than 30 characters
char buff[MAXWLEN];
int nwords, totalwords;
main() {
int i;
while(get_word(buff)) {
/**** The part where I am stuck on ****/
}
for(i = 0; i < nwords; i++)
totalwords += wordcount; //if I keep getting
//words, the loop will
//continue
printf("there were %d different words out of %d totalwords\n",
nwords, totalwords);
}
//-----ignore the section below, it defines what a word is to the
//-----program
I already have code to define to the compiler what a word is, so I
don't need help on that end. Therefore, I removed the code to save
space, which would've been at this location.
I'm trying to develop a program that will enable me to count the number
of words in a text file. As a plus, I'd like to be able to count how
many different words there are too. I have a decent start on the
program, but am quite unsure of where to move from here. I need to
malloc space for the array, but am not sure how to. Also, I believe
that strlen may come into play. Although I've browsed similar topics,
I'm also still unsure of the loop formatting to actually count the
words.
Any help would be greatly appreciated.
Thanks,
James
#include <stdio.h>
#include <stdlib.h>
#define MAXWORDS 4000 //less than 4000 total words in the
//text file
char *word[MAXWORDS];
int wordcount[MAXWORDS];
#define MAXWLEN 30 //no words larger than 30 characters
char buff[MAXWLEN];
int nwords, totalwords;
main() {
int i;
while(get_word(buff)) {
/**** The part where I am stuck on ****/
}
for(i = 0; i < nwords; i++)
totalwords += wordcount; //if I keep getting
//words, the loop will
//continue
printf("there were %d different words out of %d totalwords\n",
nwords, totalwords);
}
//-----ignore the section below, it defines what a word is to the
//-----program
I already have code to define to the compiler what a word is, so I
don't need help on that end. Therefore, I removed the code to save
space, which would've been at this location.