3 Commits
Author SHA1 Message Date
Markus Schreiber 4821287319 Teil 1 von game.c 2026-10-28 09:57:09 +01:00
Markus Schreiber 634c15ff32 Implementierung der Wortsuche und Eingabeverarbeitung 2026-10-25 09:26:34 +01:00
Markus Schreiber 85934a0661 Markus test 2026-10-13 14:49:11 +02:00
+35 -30
View File
@@ -4,44 +4,49 @@
// TODO: // TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
// Test 123
// Read words from file and store in 'words' array // Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) int readWords(FILE *file, char words[][MAX_WORD_LEN],
unsigned int maxWordCount)
{ {
int c = 0; unsigned int wordCount = 0;
int count = 0; unsigned int charIndex = 0;
int letterCount = 0; int c;
while (count < maxWordCount)
{ if (file == NULL) {
while ((c = fgetc(file)) != EOF) //Leerzeichen prüfen return 0;
{
if (isspace(c) || !isalpha(c)) {
if (letterCount > 0) {
break; //Wort fertig eingelesen
} }
continue; // mehrere Leerzeichen oder Sonderzeichen überspringen while ((c = fgetc(file)) != EOF) {
}
if (letterCount < MAX_WORD_LEN - 1) //wörter Buchstabenweise einlesen if (c == ',' || c == ';' || c == ' ' ||
{ c == '\n' || c == '\t' || c == '\r') {
words[count][letterCount] = (char)toupper(c);
letterCount++; if (charIndex > 0) {
words[wordCount][charIndex] = '\0';
wordCount++;
charIndex = 0;
if (wordCount >= maxWordCount) {
return (int)wordCount;
}
}
}
else {
if (wordCount < maxWordCount &&
charIndex < MAX_WORD_LEN - 1) {
words[wordCount][charIndex] = (char)c;
charIndex++;
}
} }
} }
if (letterCount == 0) { if (charIndex > 0 && wordCount < maxWordCount) {
break; //kein weiteres Wort gefunden words[wordCount][charIndex] = '\0';
wordCount++;
} }
words[count][letterCount] = '\0'; return (int)wordCount;
letterCount = 0;
count++;
if (c == EOF) {
break;
}
}
return count;
} }