45 lines
1.2 KiB
C
45 lines
1.2 KiB
C
#include "input.h"
|
|
#include <ctype.h>
|
|
#include <string.h>
|
|
|
|
// Read words from file and store in 'words' array
|
|
int readWords(FILE *file, char words[][MAX_WORD_LEN],
|
|
unsigned int maxWordCount) {
|
|
|
|
if (file == NULL) {
|
|
printf("File couldn't be opened...");
|
|
return 0;
|
|
}
|
|
|
|
char line[MAX_LINE_LEN]; // fehlerhaften String zwischenspeichern
|
|
unsigned int wordCount = 0; // eingelesene Wörter
|
|
|
|
while ((fgets(line, sizeof(line), file) != NULL) &&
|
|
(wordCount < maxWordCount)) { // gesamte Wörterliste einlesen
|
|
|
|
// Token initialisieren
|
|
char *token = strtok(
|
|
line, " ,;.\n"); // Trennen der Wörter bei den angegebenen Zeichen
|
|
|
|
while (token && (wordCount < maxWordCount)) {
|
|
|
|
if (*token == '\0') {
|
|
token = strtok(NULL, " ,;.\n");
|
|
continue;
|
|
}
|
|
|
|
for (int i = 0; token[i] != '\0'; i++) {
|
|
token[i] = toupper(token[i]); // Alles in Großbuchstaben
|
|
}
|
|
|
|
strcpy(words[wordCount], token); // einzelnes Wort in words array
|
|
// speichern
|
|
|
|
wordCount++; // Nächstes Wort
|
|
token = strtok(NULL, " ,;.\n"); // Nächster Token
|
|
}
|
|
}
|
|
|
|
return wordCount; // Anzahl der eingelesenen Wörter
|
|
}
|