Changed Input.c to solve All Remaining Tests

This commit is contained in:
Jonas Stamm 2025-10-24 22:16:55 +02:00
parent 000bc1ebdf
commit a1cb0f9626
2 changed files with 50 additions and 6 deletions

View File

@ -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;
}

View File

@ -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;
}