Files
Info2-Aufgaben/Startcode/input.c
T
2026-10-28 09:13:17 +01:00

48 lines
1.2 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.
// Test 123
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
int c = 0;
int count = 0;
int letterCount = 0;
while (count < maxWordCount)
{
while ((c = fgetc(file)) != EOF) //Leerzeichen prüfen
{
if (isspace(c) || !isalpha(c)) {
if (letterCount > 0) {
break; //Wort fertig eingelesen
}
continue; // mehrere Leerzeichen oder Sonderzeichen überspringen
}
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;
}