forked from schroederen/info2Praktikum-Wortsalat
input.c liest Wörter richtig ein aber problem bei zahlen würde aber fürs spiel langen
This commit is contained in:
parent
4faaafbd04
commit
4f0c599056
@ -4,9 +4,41 @@
|
||||
|
||||
// TODO:
|
||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
||||
|
||||
/**
|
||||
* @param file Zeiger auf die geöffnete Datei
|
||||
* @param words 2D-Array zum Speichern der Wörter
|
||||
* @param maxWordCount Maximale Anzahl von Wörtern, die eingelesen werden sollen
|
||||
* @return Anzahl der eingelesenen Wörter
|
||||
*/
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
|
||||
if (file == NULL || words == NULL || maxWordCount == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int wordCount = 0;
|
||||
char line[MAX_LINE_LEN];
|
||||
|
||||
// Zeilen einlesen
|
||||
while (wordCount < maxWordCount && fgets(line, MAX_LINE_LEN, file) != NULL) {
|
||||
// Zeile in Wörter aufteilen (Trennzeichen: Leerzeichen, Komma, Semikolon, Newline)
|
||||
char *token = strtok(line, " ,;\t\n\r");
|
||||
|
||||
while (token != NULL && wordCount < maxWordCount) {
|
||||
// Prüfen ob Token nicht leer ist
|
||||
if (strlen(token) > 0 && strlen(token) < MAX_WORD_LEN) {
|
||||
// Wort kopieren und in Großbuchstaben umwandeln
|
||||
strcpy(words[wordCount], token);
|
||||
for (int i = 0; words[wordCount][i] != '\0'; i++) {
|
||||
words[wordCount][i] = toupper((unsigned char)words[wordCount][i]);
|
||||
}
|
||||
wordCount++;
|
||||
}
|
||||
// Nächstes Wort
|
||||
token = strtok(NULL, " ,;\t\n\r");
|
||||
}
|
||||
}
|
||||
|
||||
return wordCount;
|
||||
}
|
||||
BIN
Start_Mac/runTests
Executable file
BIN
Start_Mac/runTests
Executable file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user