2025-11-04 16:43:46 +01:00

38 lines
1005 B
C

#include "input.h"
#include <string.h>
#include <ctype.h>
#include <stdio.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)
{
unsigned int count = 0;
char line[MAX_LINE_LEN];
while(fgets(line, sizeof(line), file)!= NULL && count < maxWordCount)
{
const char *delims = "\t\n\r ,;";
char *token = strtok(line, delims);
while (token != NULL && count < maxWordCount)
{
for(unsigned int i = 0; token[i] != '\0'; i++)
{
token[i] = toupper((unsigned char)token[i]);
}
strncpy(words[count], token, MAX_WORD_LEN -1);
words[count][MAX_WORD_LEN -1] = '\0';
count++;
token = strtok(NULL, delims); //hier
}
}
return count;
}