diff --git a/Startcode/input.c b/Startcode/input.c index ed77805..271e2ec 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -8,5 +8,46 @@ // Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + if (file == NULL || words == NULL || maxWordCount == 0) { + return 0; + } + unsigned int count = 0; + size_t length = 0; + int tooLong = 0; + int ch; + + while (count < maxWordCount && (ch = fgetc(file)) != EOF) { + + if (ch == ',' || ch == ';' || isspace((unsigned char)ch)) { + + if (length > 0 && !tooLong) { + words[count][length] = '\0'; + count++; + } + + length = 0; + tooLong = 0; + } + else if (!tooLong) { + + if (length < MAX_WORD_LEN - 1) { + words[count][length] = + (char)toupper((unsigned char)ch); + length++; + } + else { + tooLong = 1; + length = 0; + } + } + } + + if (count < maxWordCount && length > 0 && !tooLong) { + words[count][length] = '\0'; + count++; + } + + return (int)count; + } \ No newline at end of file