2025-11-04 09:11:29 +01:00

38 lines
1.3 KiB
C

#include "input.h"
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LEN 100
// 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
char zeile[500];
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) //words[zeilen also wörter][spalten also anzahl]
{
int anzahlWoerter = 0;
while(anzahlWoerter <= maxWordCount && fgets(zeile, sizeof(zeile), file)){ // eine zeile
char *wort = strtok(zeile, ",; \n");
while(wort != NULL && anzahlWoerter <= maxWordCount){
if(strlen(wort) == 0){
wort = strtok(NULL, ",; \n");
continue;
}
strncpy(words[anzahlWoerter], wort, MAX_WORD_LEN -1);
words[anzahlWoerter][MAX_WORD_LEN - 1]= '\0';
for(int i = 0; words[anzahlWoerter][i]; i++){
words[anzahlWoerter][i] = toupper((unsigned char)words[anzahlWoerter][i]); //jedes wort das eingelesen wurde wird komplett gross geschrieben, unsigned damit toupper kein neg. arg bekommt (zb umlaute)
}
anzahlWoerter++;
wort = strtok(NULL, ",; \n");
}
}
return anzahlWoerter;
}