implemented input.c (fails unit test, wegen Groß/Kleinschreibung) und game.c tests sind jetzt ok
This commit is contained in:
1 parent
ce4be9f902
commit
029eb426ab
2 files changed
+32
-15
No files matched your search
@@ -1,6 +1,7 @@
|
||||
#include "input.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// TODO:
|
||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
||||
@@ -8,5 +9,29 @@
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
int count = 0;
|
||||
char *line = calloc(MAX_LINE_LEN, sizeof(char));
|
||||
|
||||
char delimit[] = ",.;:!? \n";
|
||||
|
||||
while(fgets(line, MAX_LINE_LEN, file) != NULL && (count < maxWordCount)){
|
||||
|
||||
char *token = strtok(line, delimit);
|
||||
|
||||
while(token != NULL){
|
||||
size_t len = strlen(token);
|
||||
token[len] = '\0';
|
||||
char copy[MAX_LINE_LEN];
|
||||
strncpy(copy, token, len);
|
||||
copy[len] = '\0';
|
||||
|
||||
strncpy(words[count], copy, len);
|
||||
words[count][len] = '\0';
|
||||
count++;
|
||||
|
||||
token = strtok(NULL, delimit);
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
Reference in new issue
Block a user