second implementation for input and fix unit_tests

This commit is contained in:
hoangto105860 committed 2026-10-24 15:49:59 +02:00
1 parent aef73683bc
commit 187e626191
5 files changed
+34 -28

No files matched your search

+32 -28
View File
@@ -2,6 +2,8 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
// TODO: // 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.
@@ -9,45 +11,47 @@
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{ {
// printf("start readWords\n"); printf("start readWords\n");
int wordcount = 0; int wordcount = 0;
char line[256]; char buffer[256];
char *lines = fgets(line, sizeof(line), file); char *line = fgets(buffer, sizeof(buffer), file);
while(lines != NULL) while(line != NULL)
{ {
// printf("next line: %s\n", lines); printf("%d. loop\n", wordcount);
size_t wordLength = 0; printf("line: %s\n", line);
int newWord = 0;
for(int i = 0; i < strlen(lines); i++) char* word = strtok(line, " ,;");
{
//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]);
wordLength += 1; while (word != NULL)
newWord = 0;
}
else
{ {
if(newWord == 0) strncpy(words[wordcount], word, strlen(word));
{ //printf("%d. word: %s\n", wordcount, words[wordcount]);
// printf("skip: %c\n", line[i]);
wordcount += 1; wordcount += 1;
wordLength = 0;
newWord = 1; word = strtok(NULL, " ,;\t\n");
} }
else
line = fgets(buffer, sizeof(buffer), file);
}
for(int i = 0; i < wordcount; i++)
{ {
// printf("Multi skip: %c\n", line[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]);
} }
lines = fgets(line, sizeof(line), file);
}
// printf("wordcount = %d\n", wordcount); printf("wordcount = %d\n", wordcount);
return wordcount; return wordcount;
} }
BIN
View File
Binary file not shown.
Binary file not shown.
+2
View File
@@ -49,6 +49,8 @@ void test_readWords_with_delimiters(void) {
int count = readWords(f, words, 10); int count = readWords(f, words, 10);
fclose(f); fclose(f);
char cwd[PATH_MAX];
TEST_ASSERT_EQUAL_INT(3, count); TEST_ASSERT_EQUAL_INT(3, count);
TEST_ASSERT_EQUAL_STRING("HUND", words[0]); TEST_ASSERT_EQUAL_STRING("HUND", words[0]);
TEST_ASSERT_EQUAL_STRING("KATZE", words[1]); TEST_ASSERT_EQUAL_STRING("KATZE", words[1]);
Binary file not shown.