forked from freudenreichan/info2Praktikum-Wortsalat
56 lines
1.1 KiB
C
56 lines
1.1 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.}
|
|
|
|
|
|
// Read words from file and store in 'words' array
|
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|
{
|
|
int wordCount = 0;
|
|
char line[MAX_LINE_LEN];
|
|
|
|
while (fgets(line, MAX_LINE_LEN, file)) {
|
|
char currentWord[MAX_LINE_LEN];
|
|
int wordPos = 0;
|
|
|
|
for ( int i = 0; line[i] != '\0'; i++) {
|
|
if (line[i] == ',' || line[i] == ';' || line[i] == ' ' || line[i] == '\n') {
|
|
|
|
if (wordPos > 0) {
|
|
|
|
currentWord[wordPos] = '\0';
|
|
|
|
if (wordCount < maxWordCount) {
|
|
|
|
strcpy(words[wordCount], currentWord);
|
|
wordCount++;
|
|
}
|
|
wordPos = 0;
|
|
}
|
|
}
|
|
else {
|
|
if (wordPos < MAX_WORD_LEN-1) {
|
|
|
|
currentWord[wordPos] = toupper(line[i]);
|
|
wordPos++;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (wordPos > 0) {
|
|
|
|
currentWord[wordPos] = '\0';
|
|
|
|
if (wordCount < maxWordCount) {
|
|
|
|
strcpy(words[wordCount], currentWord);
|
|
wordCount++;
|
|
}
|
|
|
|
}
|
|
}
|
|
return wordCount;
|
|
} |