44 lines
1.2 KiB
C
44 lines
1.2 KiB
C
#include "input.h"
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <stdio.h>
|
|
|
|
// TODO: | VERSION: Erfolgreicher Test 1,2 & 3
|
|
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
|
|
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|
{
|
|
unsigned int count = 0;
|
|
int c;
|
|
unsigned int idx = 0;
|
|
|
|
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;
|
|
}
|
|
|