36 lines
1.1 KiB
C
36 lines
1.1 KiB
C
#include "input.h"
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
// TODO:
|
|
// 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)
|
|
{
|
|
char line[1024]; // Puffer für eine Zeile
|
|
unsigned int count = 0;
|
|
|
|
// Alle gewünschten Trennzeichen
|
|
const char *delimiters = " \t\n,.;:!?\"'()[]{}";
|
|
|
|
while (fgets(line, sizeof(line), file) && count < maxWordCount) {
|
|
char *token = strtok(line, delimiters);
|
|
while (token != NULL && count < maxWordCount) {
|
|
// Alles in Großbuchstaben umwandeln
|
|
for (int i = 0; token[i]; i++) {
|
|
token[i] = toupper(token[i]);
|
|
}
|
|
|
|
strncpy(words[count], token, MAX_WORD_LEN - 1);
|
|
words[count][MAX_WORD_LEN - 1] = '\0'; // Nullterminierung
|
|
count++;
|
|
|
|
token = strtok(NULL, delimiters);
|
|
}
|
|
}
|
|
|
|
fclose(file);
|
|
return count;
|
|
|
|
} |