In: Computer Science
/*
Assignment in C programming class:
read the text file line by line, and place each word, in order, in an array of char strings.
As you copy each word into the array, you will keep a running count of the number of words
in the text file and convert the letters of each word to lower case.
Assume that you will use no more than 1000 words in the text, and no more than the first
10 characters in a word. However, there is no guarantee that the text will have 1000 words
or less and any word in the text is 10 characters or less
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
// usage : textfile is to be read as command line argument
// eg: ./a.out myFile
int main(int argc, char * argv[])
{
if (argc < 2) return 1;
char * filename = argv[1];
FILE * fp = fopen(filename, "r");
if (fp == NULL) return 1;
char word[10];
int countWords = 0;
char allWords[1000][10];
/* assumes no word exceeds length of 1023 */
while (fscanf(fp, " %1023s", word) > 0) {
for(int i = 0; word[i] != '\0'; i++)
word[i] = tolower(word[i]);
strcpy(allWords[countWords], word);
countWords++;
// puts(allWords[countWords-1]);
}
return 0;
}