3 Commits
Author SHA1 Message Date
Markus Schreiber bfef59e0a6 Implementierung von game.c 2026-10-28 10:24:13 +01:00
dunkellu103850 3ab8b26e05 Input C. 2026-10-28 09:13:17 +01:00
dunkellu103850 7d518049a1 TEST 0123456789 2026-10-13 14:48:43 +02:00
+34 -39
View File
@@ -4,49 +4,44 @@
// 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], int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
unsigned int maxWordCount)
{ {
unsigned int wordCount = 0; int c = 0;
unsigned int charIndex = 0; int count = 0;
int c; int letterCount = 0;
while (count < maxWordCount)
if (file == NULL) { {
return 0; while ((c = fgetc(file)) != EOF) //Leerzeichen prüfen
} {
if (isspace(c) || !isalpha(c)) {
while ((c = fgetc(file)) != EOF) { if (letterCount > 0) {
break; //Wort fertig eingelesen
if (c == ',' || c == ';' || c == ' ' ||
c == '\n' || c == '\t' || c == '\r') {
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; continue; // mehrere Leerzeichen oder Sonderzeichen überspringen
charIndex++; }
}
if (letterCount < MAX_WORD_LEN - 1) //wörter Buchstabenweise einlesen
{
words[count][letterCount] = (char)toupper(c);
letterCount++;
}
}
if (letterCount == 0) {
break; //kein weiteres Wort gefunden
}
words[count][letterCount] = '\0';
letterCount = 0;
count++;
if (c == EOF) {
break;
} }
} }
return count;
}
if (charIndex > 0 && wordCount < maxWordCount) {
words[wordCount][charIndex] = '\0';
wordCount++;
}
return (int)wordCount;
}