Files
info2Praktikum-Wortsalat_Be…/Startcode/input.c
T
2026-10-29 14:56:44 +01:00

42 lines
1.0 KiB
C

#include "input.h"
#include <string.h>
#include <ctype.h>
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
int wordCount = 0;
int charCount = 0;
int c;
while ((c = fgetc(file)) != EOF) {
if (c == ',' || c == ';' || c == '\n' || c == '\r') {
if (charCount > 0) {
words[wordCount][charCount] = '\0';
wordCount++;
charCount = 0;
}
if (wordCount >= maxWordCount) {
break;
}
}
else if (c == ' ' || c == '\t') {
continue; // Leerzeichen und Tabs überspringen
}
else if (charCount < MAX_WORD_LEN - 1) {
words[wordCount][charCount++] = (char)toupper((unsigned char)c);
}
}
if (charCount > 0 && wordCount < maxWordCount) {
words[wordCount][charCount] = '\0';
wordCount++;
}
fclose(file);
return wordCount;
}