diff --git a/Startcode/input.c b/Startcode/input.c index ed77805..caabf2c 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -3,10 +3,54 @@ #include // TODO: -// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. +// 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) { + int wordCount = 0; + char line[MAX_LINE_LEN]; + while (fgets(line, MAX_LINE_LEN, file)) { + char currentWord[MAX_LINE_LEN]; + int wordPos = 0; + + for ( int i = 0; line[i] != '\0'; i++) { + if (line[i] == ',' || line[i] == ';' || line[i] == ' ' || line[i] == '\n') { + + if (wordPos > 0) { + + currentWord[wordPos] = '\0'; + + if (wordCount < maxWordCount) { + + strcpy(words[wordCount], currentWord); + wordCount++; + } + wordPos = 0; + } + } + else { + if (wordPos < MAX_WORD_LEN-1) { + + currentWord[wordPos] = toupper(line[i]); + wordPos++; + } + } + } + + if (wordPos > 0) { + + currentWord[wordPos] = '\0'; + + if (wordCount < maxWordCount) { + + strcpy(words[wordCount], currentWord); + wordCount++; + } + + } + } + return wordCount; } \ No newline at end of file