Files
I2-Pr/Wordsalat/input.c
T

54 lines
1.5 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)
{
// printf("start readWords\n");
int wordcount = 0;
char line[256];
char *lines = fgets(line, sizeof(line), file);
while(lines != NULL)
{
// printf("next line: %s\n", lines);
size_t wordLength = 0;
int newWord = 0;
for(int i = 0; i < strlen(lines); i++)
{
//printf("size of line %d: %d\n", i, strlen(lines));
if(isalpha(line[i]))
{
words[wordcount][wordLength] = line[i];
// printf("word[%d][%d] = %c\n", wordcount, wordLength, line[i]);
wordLength += 1;
newWord = 0;
}
else
{
if(newWord == 0)
{
// printf("skip: %c\n", line[i]);
wordcount += 1;
wordLength = 0;
newWord = 1;
}
else
{
// printf("Multi skip: %c\n", line[i]);
}
}
}
lines = fgets(line, sizeof(line), file);
}
// printf("wordcount = %d\n", wordcount);
return wordcount;
}