2025-12-02 14:21:50 +01:00

39 lines
1.2 KiB
C

#include "input.h"
#include <string.h>
#include <ctype.h>
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
unsigned int count = 0; // Anzahl übernommener Wörter
int j = 0; // aktuelle Wortlänge
int ch; // eingelesenes Zeichen
char buf[MAX_WORD_LEN]; // Puffer für ein Wort
while ((ch = fgetc(file)) != EOF) {
unsigned char c = (unsigned char)ch;
if (isalnum(c)) {
// Teil eines Wortes → Uppercase, sicher begrenzt
if (j < (MAX_WORD_LEN - 1)) {
buf[j++] = (char)toupper(c);
}
} else {
// Trennzeichen: Wort abschließen, wenn vorhanden
if (j > 0) {
buf[j] = '\0';
strncpy(words[count], buf, MAX_WORD_LEN);
count++;
j = 0;
if (count >= maxWordCount) break;
}
}
}
// Letztes Wort am Dateiende übernehmen
if (j > 0 && count < maxWordCount) {
buf[j] = '\0';
strncpy(words[count], buf, MAX_WORD_LEN);
count++;
}
return (int)count;
}