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
+39 -33

No files matched your search

+37 -33
View File
@@ -2,6 +2,8 @@
#include <string.h>
#include <ctype.h>
// 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;
}
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);
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]);
Binary file not shown.