generated from freudenreichan/info2Praktikum-Wortsalat
44 lines
1.3 KiB
C
44 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.
|
|
|
|
// Read words from file and store in 'words' array
|
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|
{
|
|
// MAX_WORD_LEN 100
|
|
// MAX_LINE_LEN 1024
|
|
// *file ist Datei
|
|
// words ist Array
|
|
// MAX_WORD_LEN ist maximale Wortlänge
|
|
// maxWordCount ist maximale Anzahl an Wörtern
|
|
char puffer[MAX_LINE_LEN];
|
|
int counter = 0;
|
|
|
|
while(fgets(puffer, MAX_LINE_LEN-1, file) != NULL && counter < maxWordCount)
|
|
{
|
|
char *parts = strtok(puffer, ",;\n\t/. ");
|
|
|
|
while(parts != NULL && counter < maxWordCount)
|
|
{
|
|
//strncpy(words[counter][MAX_WORD_LEN], puffer, MAX_LINE_LEN-1);
|
|
//words[counter][MAX_WORD_LEN-1] = "\0";
|
|
//Großbuchstaben
|
|
for(int i = 0; i < MAX_WORD_LEN -1 && parts[i] != '\0'; i++)
|
|
{
|
|
words[counter][i] = toupper(parts[i]);
|
|
words[counter][i] = '\0';
|
|
}
|
|
}
|
|
counter++; // Wort eingelesen -> Wortzähler erhöhen
|
|
parts = strtok(NULL, ",;\n\t/. ");
|
|
|
|
}
|
|
return counter;
|
|
}
|
|
|
|
|
|
|
|
//TODO: Wörter auf Zahlen, Umlaute überprüfen -> ändern
|