I'm trying to read in a file and find words that ONLY contain certain characters. For example, I want to find words that only have "ats" so "bats" or "cats" would be invalid but "ats", "stats", "sat" would be valid.
So far I've gotten my program to count how many times the characters m e l o w show up, which for my test is 37. For my test text file I have
hello
low
meow
mole
owl
dog
paper
cat
books
marshmellow
pillow
moles
Code:
int main(void) {
char TEST[5] = {'m', 'e', 'l', 'o', 'w'};
int max_words=12;
int max_len=12;
int i,k,j,letter=0, t=0;
char words[max_words][max_len];
FILE *in = NULL;
in = fopen("dummy.txt", "r"); //opens file for reading
//verify that file is open
if (in == NULL)
printf("Unable to open input file.");
//read data
for(i=0; !feof(in) && i<max_words ;i++) {
fscanf(in, "%s", &words[i][max_len]);
printf("%s\n", &words[i][max_len]);
}
//print out characters
printf("\n");
for(j=1; j<=max_words; j++) {
for(k=0;k<max_len;k++) {
if(words[j][k]!='\0'){
printf("%c.", words[j][k]);
}
else
break;
for(t=0;t<5;t++) { //count the number of times the character shows up
if(words[j][k]==TEST[t])
letter++;
}
}
printf("\n");
}
printf("\n=i%d. k=%d letter=%d", i, k, letter);
return 0;
}