41 lines
1.5 KiB
C
41 lines
1.5 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)
|
|
{
|
|
unsigned int wordCount=0;// die beiden Zähler also hier fertige wörter werden hier gespeichert
|
|
unsigned int letterCount=0;// zählt die gespeicherten zeichen der aktuellen wörter
|
|
int character;
|
|
|
|
//ungültige parameter müssen abgefragt werden
|
|
if (file == NULL || words == NULL || maxWordCount == 0) {
|
|
return 0;
|
|
}
|
|
// datei zeichen für zeichen lesen & fgetc (file get character) E0F (end of file)
|
|
while (wordCount < maxWordCount && (character = fgetc(file)) != EOF)
|
|
{
|
|
//leerräume tc trennen die wörter
|
|
if (isspace(character) || character == ',' || character == ';') {
|
|
if (letterCount > 0) {
|
|
words[wordCount] [letterCount] = '\0';
|
|
wordCount++;
|
|
letterCount=0;
|
|
}
|
|
} else if (letterCount < MAX_WORD_LEN -1 ) {
|
|
words[wordCount] [letterCount] = (char)toupper (character);// to uppercase
|
|
letterCount++;
|
|
}
|
|
}
|
|
// letzes wort speicher, auch ohne trennzeichen am datei ende
|
|
if (letterCount > 0 && wordCount < maxWordCount) {
|
|
words[wordCount] [letterCount] = '\0';
|
|
wordCount++;
|
|
}
|
|
return (int)wordCount;
|
|
}
|