You don't have problems with reading the file? What I would do is something like this: - Read the file - Count the words in the file (= word_count) - Find the longest word and it's size (= max_size) - Store this in an array of strings, which in turn are arrays of char. And for that last part:
Code:
char **word_list = (char**)malloc(word_count*sizeof(char*))
That initializes an array with length word_count. Each cell of that array is prepared to store a char array:
Code:
for(i = 0; i < word_count; i++) { word_list[i] = (char*)malloc(max_size*sizeof(char)); }
Each cell is now storing an array with length max_size. Lets say the first word is "Hello". You're now supposed to get something like this:
Code:
word_list[0][0] = "H"; word_list[0][1] = "e"; word_list[0][2] = "l"; word_list[0][3] = "l"; word_list[0][4] = "o";
To select a random word:
Code:
int randKey = rand()%word_count;
This generates a random integer between 0 and word_count-1. And to print the random word:
Code:
for(i = 0; i < max_size; i++) { printf("%c", word_list[randKey][i]; }
I may have the char identifier wrong. Or to skip the null cells:
Code:
i = 0; do { printf("%c", word_list[randKey][i]; i++; }while(word_list[randKey][i] != NULL);