Hi everyone.
I want to read a file and store each word in an array. I also want to know how many words there are in the file. After I do this, I want to reverse the words from two different files and see if they are anagrams.
Here is what i have so far...
makeWordList: identify the words and store in an array
getWordList: returns the list of words
getNumWords: returns the number of words found
findAnagrams: compares two word list and see if any word from one list is the reverse of the word in the other list. for example blah and halb
#include <math.h>
#include <stdio.h>
#include <string.h>
extern void makeWordList (char*fileName);
extern char **getWordList ();
extern int getNumWords ();
extern void findAnagrams (char*fileName1, char*fileName2);
char words [CHAR_LIMIT + 1]; /*global so I can use it in other method*/
void makeWordList(char*fileName);
{
FILE *inputFile;
char ch;
int index = 0;
inputFile = fopen (fileName, "r");
ch = fgetc (inputFile);
while (ch != EOF) {
if (ch != '\n') {
if (index >= CHAR_LIMIT) {
printf ("Character limit reached. Exiting program.\n");
exit (0);
}
words[index] = ch;
index++;
}
else {
words[index] = '\0';
index++;
}
ch = fgetc (inputFile);
}
fclose (inputFile);
}
void printWordList (char *fileName)
{
char **words;
int i, numWords;
makeWordList (fileName);
words = (char**) getWordList ();
numWords = getNumWords ();
printf ("Words in file %s\n", fileName);
for (i=0; i<numWords; i++) {
printf (" %s\n", words[i]);
}
}
int main (int argc, char **argv)
{
printWordList ("ex2test1.txt");
printWordList ("ex2test2.txt");
printWordList ("ex2test3.txt");
findReversals ("ex2test2.txt", "ex2test3.txt");
}