input funktioniert eigentlich
This commit is contained in:
parent
e3658be8a3
commit
a0523f0ad3
@ -1,3 +1,4 @@
|
|||||||
|
#include <stdio.h>
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
@ -5,8 +6,35 @@
|
|||||||
// TODO:
|
// TODO:
|
||||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
// 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)
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
|
unsigned int count = 0;
|
||||||
|
// größerer Zeilenpuffer, falls mehrere Wörter/Delimiter in einer Zeile stehen
|
||||||
|
char line[4 * MAX_WORD_LEN];
|
||||||
|
const char *DELIMS = " ,;:.-_!?\t\r\n()[]{}\"'/\\";
|
||||||
|
|
||||||
|
while (count < maxWordCount && fgets(line, sizeof(line), file)) {
|
||||||
|
// Zeilenende entfernen
|
||||||
|
line[strcspn(line, "\r\n")] = '\0';
|
||||||
|
|
||||||
}
|
// Tokenisieren nach Delimitern (auch wenn keine Spaces dazwischen sind)
|
||||||
|
for (char *tok = strtok(line, DELIMS);
|
||||||
|
tok && count < maxWordCount;
|
||||||
|
tok = strtok(NULL, DELIMS))
|
||||||
|
{
|
||||||
|
// Uppercase (ASCII-sicher)
|
||||||
|
for (char *p = tok; *p; ++p)
|
||||||
|
*p = (char)toupper((unsigned char)*p);
|
||||||
|
|
||||||
|
// Sicher kopieren + nullterminieren
|
||||||
|
strncpy(words[count], tok, MAX_WORD_LEN - 1);
|
||||||
|
words[count][MAX_WORD_LEN - 1] = '\0';
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (int)count;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user