generated from freudenreichan/info2Praktikum-Wortsalat
Implementierung der Wortsuche und Eingabeverarbeitung
This commit is contained in:
1 parent
85934a0661
commit
634c15ff32
2 files changed
+147
-3
No files matched your search
+41
-1
@@ -6,7 +6,47 @@
|
||||
// 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 wordCount = 0;
|
||||
unsigned int charIndex = 0;
|
||||
int c;
|
||||
|
||||
if (file == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
while ((c = fgetc(file)) != EOF) {
|
||||
|
||||
if (c == ',' || c == ';' || c == ' ' ||
|
||||
c == '\n' || c == '\t' || c == '\r') {
|
||||
|
||||
if (charIndex > 0) {
|
||||
words[wordCount][charIndex] = '\0';
|
||||
|
||||
wordCount++;
|
||||
charIndex = 0;
|
||||
|
||||
if (wordCount >= maxWordCount) {
|
||||
return (int)wordCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (wordCount < maxWordCount &&
|
||||
charIndex < MAX_WORD_LEN - 1) {
|
||||
|
||||
words[wordCount][charIndex] = (char)c;
|
||||
charIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (charIndex > 0 && wordCount < maxWordCount) {
|
||||
words[wordCount][charIndex] = '\0';
|
||||
wordCount++;
|
||||
}
|
||||
|
||||
return (int)wordCount;
|
||||
}
|
||||
Reference in new issue
Block a user