42 lines
1.3 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.
static void trim_and_toupper(char*dest, const char*src, int max_len) {
int len = strlen(src);
int start = 0, end = len-1;
while (start < len && isspace((unsigned char)src[start])) {
start++;
}
while (end >= start && isspace((unsigned char)src[end])) {
end--;
}
int i;
for (i = 0; i <= (end - start)&& i <(max_len-1) ; i++) {
dest[i] = toupper((unsigned char)src[start+i]);
}
dest[i] = '\0';
}
// Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) {
unsigned int count = 0;
char line_buffer[MAX_LINE_LEN];
const char* DELIMITERS=",:\n";
while (count < maxWordCount && fgets(line_buffer, MAX_LINE_LEN, file)!= NULL) {
char* token = strtok(line_buffer, DELIMITERS);
while (token != NULL && count < maxWordCount) {
trim_and_toupper(words[count], token, MAX_WORD_LEN);
if (words[count][0] !='\n'){
count++;
}
token = strtok(NULL, DELIMITERS);
}
}
return count;
}