generated from freudenreichan/info2Praktikum-Wortsalat
39 lines
1.3 KiB
C
39 lines
1.3 KiB
C
#include "input.h"
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#define MAX_WORD_LEN 100
|
|
|
|
// 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
|
|
char zeile[500];
|
|
|
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|
{
|
|
int anzahlWoerter = 0;
|
|
|
|
//1 durchlauf = 1 zeile, solange wortlimit nicht erreicht und fgets eine zeile liefern kann
|
|
while(anzahlWoerter <= maxWordCount && fgets(zeile, sizeof(zeile), file)){
|
|
char *wort = strtok(zeile, ",; \n");
|
|
|
|
//1 durchlauf = 1 wort, solange weiteres wort vorhanden und limit nicht erreicht
|
|
while(wort != NULL && anzahlWoerter <= maxWordCount){
|
|
if(strlen(wort) == 0){
|
|
wort = strtok(NULL, ",; \n");
|
|
continue;
|
|
}
|
|
|
|
strcpy(words[anzahlWoerter], wort);
|
|
|
|
//aktuelles wort gross schreiben
|
|
for(int i = 0; words[anzahlWoerter][i]; i++){
|
|
words[anzahlWoerter][i] = toupper((unsigned char)words[anzahlWoerter][i]); //unsigned damit toupper keie neg. zahlen übergeben bekommt bei zb sonderzeichen und umlauten
|
|
}
|
|
|
|
anzahlWoerter++;
|
|
wort = strtok(NULL, ",; \n");
|
|
}
|
|
}
|
|
return anzahlWoerter;
|
|
} |