generated from freudenreichan/info2Praktikum-Wortsalat
53 lines
1.3 KiB
C
53 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.
|
|
|
|
// Erkennen der Trennzeichen -> c = 1 wenn Trennzeichen
|
|
// Static ist interne Hilfsfunktion, die auserhalb nicht aufgerufen werden kann
|
|
static int istTrennzeichen (int c)
|
|
{
|
|
return c == ' ' || c == ',' || c == ';' || c == '\n' || c == '\t'|| c == '\r';
|
|
}
|
|
|
|
|
|
static int readWord(FILE *file, char *word)
|
|
{
|
|
unsigned int len = 0;
|
|
int c = fgetc(file);
|
|
|
|
// Trennzeichen vor Wort überspringen (EOF= End of File)
|
|
while(c != EOF && istTrennzeichen(c))
|
|
{
|
|
c = fgetc(file);
|
|
}
|
|
|
|
//Buchstaben sammeln bis Trennzeichen oder Dateiende
|
|
while(c != EOF && !istTrennzeichen(c))
|
|
{
|
|
if(len < MAX_WORD_LEN -1)
|
|
{
|
|
word[len] = toupper(c);
|
|
len++;
|
|
}
|
|
c = fgetc(file);
|
|
}
|
|
|
|
word[len] = '\0';
|
|
return len > 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;
|
|
|
|
while(count < maxWordCount && readWord(file, words[count]))
|
|
{
|
|
count++;
|
|
}
|
|
return count;
|
|
} |