2025-11-04 15:05:03 +01:00

39 lines
1.3 KiB
C

#include "input.h"
#include <string.h>
#include <ctype.h>
// TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
if (file == NULL)
return 0;
unsigned int count = 0;
char line[512];
// Lese Datei zeilenweise
while (fgets(line, sizeof(line), file) != NULL && count < maxWordCount)
{
// Zerlege die Zeile in Tokens (Wörter), getrennt durch gängige Delimiter
char *token = strtok(line, " ,;:.!?\"\n\r\t");
while (token != NULL && count < maxWordCount)
{
// In Großbuchstaben umwandeln
for (int i = 0; token[i]; i++)
token[i] = (char)toupper((unsigned char)token[i]);
// Wort kopieren
strncpy(words[count], token, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0';
count++;
token = strtok(NULL, " ,;:.!?\"\n\r\t");
}
}
return count;
}