2025-11-03 16:26:30 +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[256];
char *teiler = " ,;.\n";
char *token;
unsigned int wordCount = 0;
while (fgets (puffer, 256, file) != NULL) //liest words.txt ein und speichert es in puffer
{
token = strtok(puffer, teiler); //token teilt erstes Wort und speichert es in sich ab
while (token != NULL && wordCount < maxWordCount)
{
strncpy(words[wordCount], token, MAX_WORD_LEN -1); //kopiert das Wort von token in das array words
words[wordCount][MAX_WORD_LEN-1] = '\0';
// Konvertiere jedes Zeichen zu Großbuchstaben
for (int i = 0; words[wordCount][i] != '\0'; i++)
words[wordCount][i] = toupper(words[wordCount][i]);
wordCount++;
token = strtok (NULL, teiler); //nächstes Wort
}
}
return wordCount; //Anzahl der gelesenen Wörter
}