first implementation for input
This commit is contained in:
1 parent
dc58f1e4a9
commit
aef73683bc
11 files changed
+58
-39
No files matched your search
Binary file not shown.
Binary file not shown.
+42
-1
@@ -6,7 +6,48 @@
|
||||
// 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
|
||||
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
// printf("start readWords\n");
|
||||
int wordcount = 0;
|
||||
char line[256];
|
||||
char *lines = fgets(line, sizeof(line), file);
|
||||
|
||||
}
|
||||
while(lines != NULL)
|
||||
{
|
||||
// printf("next line: %s\n", lines);
|
||||
size_t wordLength = 0;
|
||||
int newWord = 0;
|
||||
|
||||
for(int i = 0; i < strlen(lines); i++)
|
||||
{
|
||||
//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;
|
||||
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);
|
||||
}
|
||||
// printf("wordcount = %d\n", wordcount);
|
||||
return wordcount;
|
||||
}
|
||||
Binary file not shown.
+2
-1
@@ -10,6 +10,7 @@
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int exitCode = EXIT_SUCCESS;
|
||||
printf("start main\n");
|
||||
|
||||
// Check if the correct number of arguments is provided
|
||||
if(argc != 2)
|
||||
@@ -51,4 +52,4 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
return exitCode;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Executable
BIN
Binary file not shown.
+13
-36
@@ -32,51 +32,27 @@ void print_test_result(int result) {
|
||||
void test_readWords_simple(void) {
|
||||
FILE *f = fopen("testwords_simple.txt", "r");
|
||||
|
||||
char words[14][MAX_WORD_LEN];
|
||||
int count = readWords(f, words, 14);
|
||||
char words[10][MAX_WORD_LEN];
|
||||
int count = readWords(f, words, 10);
|
||||
fclose(f);
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(15, count);
|
||||
TEST_ASSERT_EQUAL_STRING("Yeti", words[0]);
|
||||
TEST_ASSERT_EQUAL_STRING("Nessie", words[1]);
|
||||
TEST_ASSERT_EQUAL_STRING("Werwolf", words[2]);
|
||||
TEST_ASSERT_EQUAL_STRING("Vampir", words[3]);
|
||||
TEST_ASSERT_EQUAL_STRING("Monster", words[4]);
|
||||
TEST_ASSERT_EQUAL_STRING("Hydra", words[5]);
|
||||
TEST_ASSERT_EQUAL_STRING("Frankenstein", words[6]);
|
||||
TEST_ASSERT_EQUAL_STRING("Dracula", words[7]);
|
||||
TEST_ASSERT_EQUAL_STRING("KingKong", words[8]);
|
||||
TEST_ASSERT_EQUAL_STRING("Gremlin", words[9]);
|
||||
TEST_ASSERT_EQUAL_STRING("Kobold", words[10]);
|
||||
TEST_ASSERT_EQUAL_STRING("Hexe", words[11]);
|
||||
TEST_ASSERT_EQUAL_STRING("Poltergeist", words[12]);
|
||||
TEST_ASSERT_EQUAL_STRING("Gespenst", words[13]);
|
||||
TEST_ASSERT_EQUAL_STRING("Oger", words[14]);
|
||||
TEST_ASSERT_EQUAL_INT(3, count);
|
||||
TEST_ASSERT_EQUAL_STRING("APFEL", words[0]);
|
||||
TEST_ASSERT_EQUAL_STRING("BANANE", words[1]);
|
||||
TEST_ASSERT_EQUAL_STRING("KIWI", words[2]);
|
||||
}
|
||||
|
||||
void test_readWords_with_delimiters(void) {
|
||||
FILE *f = fopen("testwords_delims.txt", "r");
|
||||
|
||||
char words[14][MAX_WORD_LEN];
|
||||
int count = readWords(f, words, 14);
|
||||
char words[10][MAX_WORD_LEN];
|
||||
int count = readWords(f, words, 10);
|
||||
fclose(f);
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(15, count);
|
||||
TEST_ASSERT_EQUAL_STRING("Yeti", words[0]);
|
||||
TEST_ASSERT_EQUAL_STRING("Nessie", words[1]);
|
||||
TEST_ASSERT_EQUAL_STRING("Werwolf", words[2]);
|
||||
TEST_ASSERT_EQUAL_STRING("Vampir", words[3]);
|
||||
TEST_ASSERT_EQUAL_STRING("Monster", words[4]);
|
||||
TEST_ASSERT_EQUAL_STRING("Hydra", words[5]);
|
||||
TEST_ASSERT_EQUAL_STRING("Frankenstein", words[6]);
|
||||
TEST_ASSERT_EQUAL_STRING("Dracula", words[7]);
|
||||
TEST_ASSERT_EQUAL_STRING("KingKong", words[8]);
|
||||
TEST_ASSERT_EQUAL_STRING("Gremlin", words[9]);
|
||||
TEST_ASSERT_EQUAL_STRING("Kobold", words[10]);
|
||||
TEST_ASSERT_EQUAL_STRING("Hexe", words[11]);
|
||||
TEST_ASSERT_EQUAL_STRING("Poltergeist", words[12]);
|
||||
TEST_ASSERT_EQUAL_STRING("Gespenst", words[13]);
|
||||
TEST_ASSERT_EQUAL_STRING("Oger", words[14]);
|
||||
TEST_ASSERT_EQUAL_INT(3, count);
|
||||
TEST_ASSERT_EQUAL_STRING("HUND", words[0]);
|
||||
TEST_ASSERT_EQUAL_STRING("KATZE", words[1]);
|
||||
TEST_ASSERT_EQUAL_STRING("MAUS", words[2]);
|
||||
}
|
||||
|
||||
void test_readWords_empty_file(void) {
|
||||
@@ -195,3 +171,4 @@ int main(void) {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,4 +2,4 @@ Yeti,Nessie Werwolf; Vampir
|
||||
Monster
|
||||
Hydra;Frankenstein
|
||||
Dracula;KingKong;Gremlin;Kobold,Hexe;Poltergeist
|
||||
Gespenst, Oger
|
||||
Gespenst, Oger
|
||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Reference in new issue
Block a user