49 lines
1.0 KiB
C
49 lines
1.0 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)
|
|
{
|
|
int wordcount = 0;
|
|
char buffer[256];
|
|
char *line = fgets(buffer, sizeof(buffer), file);
|
|
|
|
while(line != NULL)
|
|
{
|
|
char* word = strtok(line, " ,;");
|
|
|
|
while (word != NULL)
|
|
{
|
|
strncpy(words[wordcount], word, strlen(word));
|
|
|
|
wordcount += 1;
|
|
|
|
word = strtok(NULL, " ,;\t\n");
|
|
}
|
|
|
|
line = fgets(buffer, sizeof(buffer), file);
|
|
}
|
|
|
|
for(int i = 0; i < wordcount; i++)
|
|
{
|
|
for(int j = 0; j < strlen(words[i]); j++)
|
|
{
|
|
words[i][j] = toupper(words[i][j]);
|
|
|
|
if(words[i][j] == '\n')
|
|
{
|
|
words[i][j] = '\0';
|
|
}
|
|
}
|
|
}
|
|
|
|
return wordcount;
|
|
}
|