From a1cb0f9626520af71fa293ee3469d6e881193f5e Mon Sep 17 00:00:00 2001 From: stammjo100588 Date: Fri, 24 Oct 2025 22:16:55 +0200 Subject: [PATCH] Changed Input.c to solve All Remaining Tests --- Start_Mac/input.c | 28 +++++++++++++++++++++++++--- Start_Windows/input.c | 28 +++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/Start_Mac/input.c b/Start_Mac/input.c index 2bffda5..935a6e9 100644 --- a/Start_Mac/input.c +++ b/Start_Mac/input.c @@ -9,13 +9,35 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { unsigned int count = 0; + int c; + unsigned int idx = 0; - while (count < maxWordCount && fscanf(file, "%s", words[count]) == 1) { - for (int i = 0; words[count][i]; i++) { - words[count][i] = toupper((unsigned char)words[count][i]); + if (!file) return 0; + + while ((c = fgetc(file)) != EOF && count < maxWordCount) { + if (isalpha((unsigned char)c)) { + if (idx < MAX_WORD_LEN - 1) { + words[count][idx++] = (char) toupper((unsigned char)c); + } else { + // word too long: truncate remaining letters until delimiter + idx = MAX_WORD_LEN - 1; + } + } else { + if (idx > 0) { + words[count][idx] = '\0'; + count++; + idx = 0; + } + // skip consecutive delimiters } + } + + // If file ended while reading a word, terminate and count it + if (idx > 0 && count < maxWordCount) { + words[count][idx] = '\0'; count++; } return count; } + diff --git a/Start_Windows/input.c b/Start_Windows/input.c index 2bffda5..935a6e9 100644 --- a/Start_Windows/input.c +++ b/Start_Windows/input.c @@ -9,13 +9,35 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { unsigned int count = 0; + int c; + unsigned int idx = 0; - while (count < maxWordCount && fscanf(file, "%s", words[count]) == 1) { - for (int i = 0; words[count][i]; i++) { - words[count][i] = toupper((unsigned char)words[count][i]); + if (!file) return 0; + + while ((c = fgetc(file)) != EOF && count < maxWordCount) { + if (isalpha((unsigned char)c)) { + if (idx < MAX_WORD_LEN - 1) { + words[count][idx++] = (char) toupper((unsigned char)c); + } else { + // word too long: truncate remaining letters until delimiter + idx = MAX_WORD_LEN - 1; + } + } else { + if (idx > 0) { + words[count][idx] = '\0'; + count++; + idx = 0; + } + // skip consecutive delimiters } + } + + // If file ended while reading a word, terminate and count it + if (idx > 0 && count < maxWordCount) { + words[count][idx] = '\0'; count++; } return count; } +