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 <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, " ,;");
while (word != NULL)
{ {
//printf("size of line %d: %d\n", i, strlen(lines)); strncpy(words[wordcount], word, strlen(word));
if(isalpha(line[i])) //printf("%d. word: %s\n", wordcount, words[wordcount]);
{ wordcount += 1;
words[wordcount][wordLength] = line[i];
// printf("word[%d][%d] = %c\n", wordcount, wordLength, line[i]);
wordLength += 1; word = strtok(NULL, " ,;\t\n");
newWord = 0; }
}
else line = fgets(buffer, sizeof(buffer), file);
{
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);
} }
// 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; 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.