diff --git a/Startcode/input.c b/Startcode/input.c index ed77805..13992f3 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -4,9 +4,55 @@ // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. +int splitWords(char texRow[], int maxLength, int wordNumber, char words[][MAX_WORD_LEN], unsigned int maxWordCount); +// Read words from file -// Read words from file and store in 'words' array int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { -} \ No newline at end of file + // Datei Zeilenweise auslesen + char textRow[MAX_LINE_LEN]; + int row = 0; + + while (fgets(textRow, MAX_LINE_LEN, file) != NULL) + row = splitWords(textRow, MAX_LINE_LEN, row, words, maxWordCount); + + fclose(file); + + for (int i; i < row; i++) + printf("%s\n", words[i]); + + return row; +} + +// Zeilen in lesbare Wörter aufteilen und "words" zuweisen +int splitWords(char textRow[], int maxStringLength, int row, char words[][MAX_WORD_LEN], unsigned int maxWordCount) +{ + char *teiler = " .,\n;"; + char *token = strtok(textRow, teiler); + + if (token != NULL) + { + if (row < maxWordCount) + { + strncpy(words[row], token, maxStringLength); + words[row][maxStringLength - 1] = '\0'; + row++; + } + } + + while (token != NULL && row < maxWordCount) + { + + token = strtok(NULL, teiler); + + if (token != NULL) + { + strncpy(words[row], token, maxStringLength); + words[row][maxStringLength - 1] = '\0'; + row++; + } + } + + return row; +}