61 lines
1.8 KiB
C
61 lines
1.8 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,.;:!?\"'()[]{}";
|
||
|
||
// Zeile für Zeile lesen
|
||
while (fgets(line, sizeof(line), file) != NULL && count < maxWordCount) {
|
||
// Tokenize Zeile nach Trennzeichen
|
||
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]);
|
||
|
||
// Prüfen, ob das Wort nur A-Z enthält
|
||
|
||
int isValidWord(const char *word)
|
||
{
|
||
for (int i = 0;word[i]; i++)
|
||
{
|
||
if (!('A' <= word[i] && word[i] <= 'Z'))
|
||
{
|
||
return 0;
|
||
}
|
||
}
|
||
return 1;
|
||
}
|
||
|
||
if (!isValidWord(token))
|
||
{
|
||
fprintf(stderr, "Ungültiges Wort gefunden: '%s'. Nur Buchstaben A-Z erlaubt.\n", token);
|
||
token = strtok(NULL, delimiters);
|
||
continue; // Wort ignorieren
|
||
}
|
||
}
|
||
|
||
// Wort ins Array kopieren
|
||
strncpy(words[count], token, MAX_WORD_LEN - 1);
|
||
words[count][MAX_WORD_LEN - 1] = '\0';
|
||
count++;
|
||
|
||
token = strtok(NULL, delimiters);
|
||
}
|
||
}
|
||
|
||
return count; // Datei nicht schließen – gehört dem Aufrufer
|
||
} |