diff --git a/Wordsalat/input.c b/Wordsalat/input.c index 94bd177..9386325 100644 --- a/Wordsalat/input.c +++ b/Wordsalat/input.c @@ -2,6 +2,8 @@ #include #include + + // TODO: // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. @@ -9,45 +11,47 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) { - // printf("start readWords\n"); + printf("start readWords\n"); int wordcount = 0; - char line[256]; - char *lines = fgets(line, sizeof(line), file); + char buffer[256]; + char *line = fgets(buffer, sizeof(buffer), file); - while(lines != NULL) + while(line != NULL) { - // printf("next line: %s\n", lines); - size_t wordLength = 0; - int newWord = 0; + printf("%d. loop\n", wordcount); + printf("line: %s\n", line); - for(int i = 0; i < strlen(lines); i++) + char* word = strtok(line, " ,;"); + + while (word != NULL) { - //printf("size of line %d: %d\n", i, strlen(lines)); - if(isalpha(line[i])) - { - words[wordcount][wordLength] = line[i]; - // printf("word[%d][%d] = %c\n", wordcount, wordLength, line[i]); + strncpy(words[wordcount], word, strlen(word)); + //printf("%d. word: %s\n", wordcount, words[wordcount]); + wordcount += 1; - wordLength += 1; - newWord = 0; - } - else - { - if(newWord == 0) - { - // printf("skip: %c\n", line[i]); - wordcount += 1; - wordLength = 0; - newWord = 1; - } - else - { - // printf("Multi skip: %c\n", line[i]); - } - } - } - lines = fgets(line, sizeof(line), file); + word = strtok(NULL, " ,;\t\n"); + } + + line = fgets(buffer, sizeof(buffer), file); } - // printf("wordcount = %d\n", wordcount); + + for(int i = 0; i < wordcount; i++) + { + for(int j = 0; j < strlen(words[i]); j++) + { + words[i][j] = toupper(words[i][j]); + + if(words[i][j] == '\n') + { + words[i][j] = '\0'; + } + } + + printf("%d. word: %s\n", i, words[i]); + } + + + printf("wordcount = %d\n", wordcount); + return wordcount; } diff --git a/Wordsalat/input.o b/Wordsalat/input.o index 6ffc05c..f8c84e6 100644 Binary files a/Wordsalat/input.o and b/Wordsalat/input.o differ diff --git a/Wordsalat/runTests b/Wordsalat/runTests index a23a6ae..7a63686 100755 Binary files a/Wordsalat/runTests and b/Wordsalat/runTests differ diff --git a/Wordsalat/unit_tests.c b/Wordsalat/unit_tests.c index 8f2290a..f385be2 100644 --- a/Wordsalat/unit_tests.c +++ b/Wordsalat/unit_tests.c @@ -49,6 +49,8 @@ void test_readWords_with_delimiters(void) { int count = readWords(f, words, 10); fclose(f); + char cwd[PATH_MAX]; + TEST_ASSERT_EQUAL_INT(3, count); TEST_ASSERT_EQUAL_STRING("HUND", words[0]); TEST_ASSERT_EQUAL_STRING("KATZE", words[1]); diff --git a/Wordsalat/wordsalad b/Wordsalat/wordsalad index 5f966b3..42a8105 100755 Binary files a/Wordsalat/wordsalad and b/Wordsalat/wordsalad differ