2025-11-07 18:27:36 +01:00

35 lines
1.2 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)
{
char puffer[MAX_LINE_LEN]; //Array für eingelesene Zeile
int counter = 0; //Zähler für Anzahl eingelesener Wörter
int i;
while(fgets(puffer, MAX_LINE_LEN-1, file) != NULL && counter < maxWordCount)
{
char *word_parts = strtok(puffer, ",;\n\t/. "); //Wörter aufteilen
while(word_parts != NULL && counter < maxWordCount)
{
// Kopiere das Wort in words-Array
strncpy(words[counter], word_parts, MAX_WORD_LEN - 1);
words[counter][MAX_WORD_LEN - 1] = '\0';
// Alles in Großbuchstaben umwandeln
for(i = 0; words[counter][i] != '\0'; i++)
{
words[counter][i] = toupper(words[counter][i]);
}
counter++; // Wort eingelesen, Wortzähler erhöhen
word_parts = strtok(NULL, ",;\n\t/. "); //NULL für Zeiger angeben
}
}
return counter;
}