74 lines
1.6 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)
{
char text [2000];
char line[200];
int i = 0;
int textcount = 0;
int counter = 0;
char *token;
//einlesen der textdatei in text[] (unterhalb)
while (fgets(line, sizeof(line), file)) {
for (int j = 0; line[j] != '\0'&& i < sizeof(text)-1;j++) {
text[i++] = line[j];
textcount++;
if (textcount>1999) {
break;
}
}
if (textcount>1999) {
printf("Textdatei zu gross!!!");
return 1;
}
}
text[i] = '\0';
//sortierung der wörter in words(unterhalb)
const char *trenner = ",; \n";
token = strtok(text, trenner);
while (token != NULL&& counter < maxWordCount) {
strncpy(words[counter], token, MAX_WORD_LEN - 1);
words[counter][MAX_WORD_LEN - 1] = '\0';
token = strtok(NULL, trenner);
counter++;
}
//alle Buchstaben in Großbuchstaben ändern
for (int h = 0; h < counter; h++) {
for (int check = 0; words[h][check] != '\0'; check++) {
if (words[h][check] >= 'a' && words[h][check] <= 'z') {
words[h][check] -= 32; // direkt ins Array schreiben
}
}
}
return counter;
}