62 lines
1.6 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)
{
unsigned int count = 0;
char line[MAX_LINE_LEN];
const char teiler[] = " ,;\n\t"; //literale
char *token = NULL;
while(fgets(line, sizeof(line), file)){
//Yeti,Nessie Werwolf; Vampir
// token = Yeti
token = strtok(line, teiler); //besser wäre: strpbrk
while (token != NULL)
{
if (strlen(token) >= MAX_WORD_LEN) {
printf("Fehler: eigelesenes Wort ist zu lang\n");
return count;
}
if (count >= maxWordCount){
printf("Fehler: zu viele Wörter\n");
return count;
}
//hier müsste man den token string in großbuchstaben formatieren
for (int i = 0; i < strlen(token); i++)
{
token[i] = toupper(token[i]);
}
strncpy(words[count], token, MAX_WORD_LEN-1);
words[count][MAX_WORD_LEN-1] = '\0';
//Nessie Werwolf; Vampir
//token= Nessie
token = strtok(NULL, teiler);
count++;
}
}
return count;
}
// 00M 01a 02x
// 10W 11a 12l 13t 14e 15r
// 20T 21o 22b 23i 24a 25s
// array[3][2]
// 00 01
// 10 11
// 20 21