Compare commits

..

No commits in common. "69f9945fe0110ef182ced2732b3bb053e3941352" and "26bc089865d6498a87f5b801169a0b5ad3ada2db" have entirely different histories.

3 changed files with 6 additions and 43 deletions

6
.gitignore vendored
View File

@ -6,8 +6,4 @@ main.o
graphicalGame.o graphicalGame.o
wordsalad wordsalad
wordsalad_initial.exe wordsalad_initial.exe
runTests.exe runTests.exe
testwords_delims.txt
testwords_empty.txt
testwords_simple.txt
.vscode

View File

@ -1,5 +1,4 @@
#include "input.h" #include "input.h"
#include <stdio.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
@ -7,39 +6,7 @@
// 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.
// Read words from file and store in 'words' array // Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{ {
char buffer[MAX_WORD_LEN];
unsigned int count = 0;
if (file == NULL) }
{
return 1;
}
while (fgets(buffer, sizeof(buffer), file) != NULL)
{
// Zerlege Zeile in einzelne Wörter anhand der Trennzeichen
char *token = strtok(buffer, " ,;\n");
while (token != NULL && count < maxWordCount)
{
// In Großbuchstaben umwandeln
for (int i = 0; token[i] != '\0'; i++)
{
token[i] = toupper((unsigned char)token[i]);
}
// Token in das words-Array kopieren
strncpy(words[count], token, MAX_WORD_LEN - 1);
words[count][MAX_WORD_LEN - 1] = '\0'; // sicher terminieren
count++;
// Nächsten Token holen
token = strtok(NULL, " ,;\n");
}
}
return count;
}

View File

@ -13,8 +13,7 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
char buffer[MAX_WORD_LEN]; char buffer[MAX_WORD_LEN];
unsigned int count = 0; unsigned int count = 0;
if (file == NULL) if (file == NULL) {
{
return 1; return 1;
} }
@ -25,8 +24,7 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
while (token != NULL && count < maxWordCount) while (token != NULL && count < maxWordCount)
{ {
// In Großbuchstaben umwandeln // In Großbuchstaben umwandeln
for (int i = 0; token[i] != '\0'; i++) for (int i = 0; token[i] != '\0'; i++) {
{
token[i] = toupper((unsigned char)token[i]); token[i] = toupper((unsigned char)token[i]);
} }
@ -41,5 +39,7 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
} }
} }
fclose(file);
return count; return count;
} }