47 lines
1.7 KiB
C

#include "input.h"
#include <string.h>
#include <ctype.h>
void readWordsFromLine(char buffer[], int* wordCount, char words[][MAX_WORD_LEN], unsigned int maxWords) {
char *token;
char *word_seperator = " ,;";
token = strtok(buffer, word_seperator);
while (*wordCount < maxWords && token != NULL) { //condition for reading new words from buffer
if (strlen(token) <= MAX_WORD_LEN) { //checks if word fits into wordsalad grid
for (int i = 0; i < strlen(token); i++) { //writes words char by char into array, stops at '\n'
if (token[i] == '\n') {
break;
} else if (token[i] >= 97 && token[i] <= 122) { //converts lower case to upper case
token[i] = token[i] - 32;
}
words[*wordCount][i] = token[i];
}
(*wordCount)++;
}
token = strtok(NULL, word_seperator);
}
}
// 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)
{
for (int i = 0; i < maxWordCount; i++) {
for (int j = 0; j < MAX_WORD_LEN; j++) {
words[i][j] = '\0';
}
}
char buffer[MAX_LINE_LEN]; // prepares a large enough buffer to read one line from the .txt-file
int readwords = 0;
int *wordCount = &readwords;
while (*wordCount < maxWordCount && fgets(buffer, MAX_LINE_LEN, file) != NULL) {
readWordsFromLine(buffer, wordCount, words, maxWordCount);
}
printf("%d\n", readwords);
return *wordCount;
}