2025-11-02 16:56:58 +01:00

45 lines
1.0 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) {
perror("File couldn't be opened");
return 0;
}
char line[MAX_LINE_LEN];
unsigned int wordCount = 0;
while ((fgets(line, sizeof(line), file) != NULL) &&
(wordCount < maxWordCount)) {
// Token initialisieren
char *token = strtok(line, " ,;.\n");
while (token && (wordCount < maxWordCount)) {
if (*token == '\0') {
token = strtok(NULL, " ,;.\n");
continue;
}
// Token in Großbuchstaben konvertieren
for (int i = 0; token[i] != '\0'; i++) {
token[i] = toupper(token[i]);
}
// In das words-Array kopieren
strcpy(words[wordCount], token);
wordCount++; // Nächstes Wort
token = strtok(NULL, " ,;.\n"); // Nächstes Token
}
}
return wordCount; // Anzahl der eingelesenen Wörter
}