Changed Input.c to solve All Remaining Tests
This commit is contained in:
parent
000bc1ebdf
commit
a1cb0f9626
@ -9,13 +9,35 @@
|
|||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
unsigned int count = 0;
|
unsigned int count = 0;
|
||||||
|
int c;
|
||||||
|
unsigned int idx = 0;
|
||||||
|
|
||||||
while (count < maxWordCount && fscanf(file, "%s", words[count]) == 1) {
|
if (!file) return 0;
|
||||||
for (int i = 0; words[count][i]; i++) {
|
|
||||||
words[count][i] = toupper((unsigned char)words[count][i]);
|
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++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -9,13 +9,35 @@
|
|||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
unsigned int count = 0;
|
unsigned int count = 0;
|
||||||
|
int c;
|
||||||
|
unsigned int idx = 0;
|
||||||
|
|
||||||
while (count < maxWordCount && fscanf(file, "%s", words[count]) == 1) {
|
if (!file) return 0;
|
||||||
for (int i = 0; words[count][i]; i++) {
|
|
||||||
words[count][i] = toupper((unsigned char)words[count][i]);
|
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++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user