This program should prompt the user for a series of letters. Then read in the letters and print out which dictionary words can be made from the provided letters. The dictionary is provided as a text file in the folder. But whenever I run it it doesn't give be anything. Can anyone help me figure out why this is happening?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 99
#define NUM_WORDS 100
void find_frequency(char string[], int count[]);
int compare_arrays(int dictionary[], int user[]);
int main()
{
int total_words=11; //number of words
char dictionary_words[NUM_WORDS][SIZE]; //store words in directory
FILE *cfPtr; //dictionary.txt pointer
if ((cfPtr=fopen("dictionary.txt","r"))==NULL)//try to open file
{
puts("File dictionary.txt could not be opened.");
exit(1);//exit if file doesn't open
}
else{ //Read each word from the dictionary and save to array
char line[SIZE]; //save each word
{
while(fgets(line,SIZE,cfPtr)!= NULL)
{
char*tokenPtr=strtok(line, "\t");
while(tokenPtr != NULL)
{
strcpy(dictionary_words[total_words],tokenPtr);
total_words++;
tokenPtr = strtok(NULL, "\t" );
}
}
}
}
fclose(cfPtr);//close file
char string[11];//get string of characters from user
int count[26]={0};//store the number of each letter
printf("Enter letters:\n");
gets(string);
find_frequency(string, count);//count of each character entered
char temp[SIZE];
int temp_count[26]={0};//convert words into letters
int i;
for(i=0; i<=total_words; i++);
{
strcpy(temp,dictionary_words[i]);
find_frequency(temp,temp_count);//convert word to letters in alphabet
if (compare_arrays(temp_count,count))//compare words with letters entered
{
printf("%s:", temp);//print what you can spell
}
else
{
printf("broken", temp);
}
memset(temp_count,0,sizeof(temp_count));//test next word
}
return(0);
}//end main
//define function
void find_frequency(char string[],int count[])
{
int i;
for(i=0; string[i] != '\0'; i++)
{
if (string[i] >= 'a' && string[i] <= 'z')
{
count[string[i]-'a']++;
}
}
}
int compare_arrays(int dictionary[], int user[])
{
int j = 0;
if (user[j] >= dictionary[j])
{
j++;
if (j == 26)
{
return 0;
}
}
return 1;
}