2025-10-28 17:26:55 +01:00

38 lines
1.0 KiB
C

#include "input.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.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 zeile[MAX_LINE_LEN];
char* teiler = ".;, ";
int wordCount = 0;
char* token;
while(fgets(zeile, MAX_LINE_LEN, file)!=NULL)
{
for (int i = 0; i < 1024; i++)
{
if (zeile[i] >= 'a'&&zeile[i] <= 'z')
{
zeile[i]=zeile[i]-32;
}
if (zeile[i] == '\n')
{
zeile[i] = '\0';
break;
}
}
token = strtok(zeile,teiler);
while(token != NULL && wordCount <= maxWordCount)
{
strcpy(words[wordCount],token);
token = strtok(NULL,teiler);
wordCount++;
}
}
return wordCount;
}