Files
Wortsalat/Startcode/input.c
T

45 lines
1.3 KiB
C

#include "input.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.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 count = 0;
char *line = calloc(MAX_LINE_LEN, sizeof(char));
char delimit[] = ",.;:!? \n";
while(fgets(line, MAX_LINE_LEN, file) != NULL && (count < maxWordCount)){
char *token = strtok(line, delimit);
while(token != NULL){
size_t len = strlen(token);
token[len] = '\0';
char copy[MAX_LINE_LEN];
strncpy(copy, token, len);
copy[len] = '\0';
strncpy(words[count], copy, len);
words[count][len] = '\0';
count++;
//schaue ob auch alles groß geschrieben ist
for(int i = 0; i < len; i++){
if(words[count][i] <= 122 && words[count][i] >= 97){
//mache Kleinbuchstaben zu Großbuchstaben //ASCII A = 65, a = 97, z = 122
words[count][i] -= 32;
}
}
token = strtok(NULL, delimit);
}
}
return count;
}