diff --git a/Startcode/input.c b/Startcode/input.c index ed77805..747448f 100644 --- a/Startcode/input.c +++ b/Startcode/input.c @@ -1,12 +1,55 @@ #include "input.h" #include #include +#include // 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 +static int createSingleWords(char text[MAX_LINE_LEN], char words[][MAX_WORD_LEN], unsigned int maxWordCount); + int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { + int wordCount = 0; + int i = 0; + char lineRead[MAX_LINE_LEN]; + while(fgets(lineRead, MAX_LINE_LEN, file) != NULL && i < maxWordCount) + { + wordCount += createSingleWords(lineRead, words, maxWordCount); + i++; + } + + return wordCount; +} + +static int createSingleWords(char text[MAX_LINE_LEN], char words[][MAX_WORD_LEN], unsigned int maxWordCount) +{ + char *teiler = " ;,.\n"; + char *token; + + int i = 0; + + token = strtok(text, teiler); + + while(token != NULL && i < maxWordCount) + { + strncpy(words[i], token, MAX_WORD_LEN - 1); + //kleinbuchstaben --> GROßBUCHSTABEN + int length = strlen(words[i]); + for(int j = 0; j < length; i++) + { + if(islower(words[i][j])) + { + words[i][j] = toupper(words[i][j]); + } + } + words[i][MAX_WORD_LEN - 1] = '\0'; + + token = strtok(NULL, teiler); + i++; + } + + return i; } \ No newline at end of file