Well I was told that the function should return the number of times that the word is found in the text file, so shouldn't it be an int?
And I just realized that the words I'm looking for aren't stored anywhere in my main. I just tried to put the words into an array but it says that there are "too many characters in constant".
Code:
#include <stdio.h>
#define MAXCHAR 30
#define MAXWORDS 30
int check_word(char word_list[][MAXCHAR], int num_words, char word[]);
void main(void)
{
FILE *infile = NULL;
char infilename[MAXWORDS][MAXCHAR];
char line[MAXCHAR];
char words[] = {'for','if','else','switch','while','case',
'break','main','int','float','char','double');
int text[MAXWORDS];
int i,d,length,c,k=0;
printf("Program to find specific words in a file\n");
while(infile == NULL) { /* input file name to read open */
printf("Input filename:");
scanf("%s",infilename);
if((infile = fopen(infilename, "r")) == NULL) {
printf("ERROR: file %s can not be opened!\n",infilename);
}
}
printf("TEST 1\n");
while(fgets(line,MAXCHAR,infile) != NULL) {
length = strlen(line);
if(line[length-1] == '\n') { /* changes new line to null character */
line[length-1] = '\0';
length--;
}
printf("TEST 2\n");
for(d=0;d<MAXWORDS;d++){
text[d] = check_word(infilename[MAXWORDS][MAXCHAR],MAXWORDS,words[MAXCHAR]);
}
printf("TEST 3\n");
for(d=0;d<MAXWORDS;d++){
printf("dictionary word %d \"%s\" occurs %d times",d,words[d],text[i]);
}
}
fclose(infile);
}
Code:
#define MAXCHAR 30
#define MAXWORDS 30
#define NOT_FOUND -1
int check_word(char word_list[][MAXCHAR], int num_words, char words[])
{
int i;
for (i=0; i<num_words;i++)
if(strncmp(word_list[i],words[i],num_words) == 0){
return (i);
}
return (NOT_FOUND);
}