38 lines
1.0 KiB
C
38 lines
1.0 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) {
|
|
char zeile[MAX_LINE_LEN];
|
|
char* teiler = ".;, ";
|
|
int wordCount = 0;
|
|
char* token;
|
|
while(fgets(zeile, MAX_LINE_LEN, file)!=NULL)
|
|
{
|
|
for (int i = 0; i < MAX_LINE_LEN; i++)
|
|
{
|
|
if (zeile[i] >= 'a'&&zeile[i] <= 'z')
|
|
{
|
|
zeile[i]=zeile[i]-32;
|
|
}
|
|
if (zeile[i] == '\n')
|
|
{
|
|
zeile[i] = '\0';
|
|
break;
|
|
}
|
|
}
|
|
token = strtok(zeile,teiler);
|
|
while(token != NULL && wordCount <= maxWordCount)
|
|
{
|
|
strcpy(words[wordCount],token);
|
|
token = strtok(NULL,teiler);
|
|
wordCount++;
|
|
}
|
|
}
|
|
return wordCount;
|
|
} |