2025-11-02 19:41:46 +01:00

64 lines
2.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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 isValidWord(const char *word)
{
for (int i = 0;word[i]; i++)
{
if (!('A' <= word[i] && word[i] <= 'Z'))
{
return 0;
}
}
return 1;
}
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)
{
// Überspringe leere Tokens
if (token && strlen(token) > 0)
{
// 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
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
}