forked from freudenreichan/info2Praktikum-Wortsalat
Funktioniert
This commit is contained in:
1 parent
6c72054f42
commit
b0951bcb45
3 files changed
+34
-10
No files matched your search
+29
-7
@@ -2,19 +2,41 @@
|
||||
#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)
|
||||
{
|
||||
int wordCount = 0;
|
||||
int charCount = 0;
|
||||
int c;
|
||||
|
||||
while ((c = fgetc(file)) != EOF) {
|
||||
|
||||
unsigned int wordCount = 0;
|
||||
while(fgets(words[wordCount],MAX_WORD_LEN,file) != NULL && wordCount+1 < maxWordCount){
|
||||
if (c == ',' || c == ';' || c == '\n' || c == '\r') {
|
||||
|
||||
if (charCount > 0) {
|
||||
words[wordCount][charCount] = '\0';
|
||||
wordCount++;
|
||||
charCount = 0;
|
||||
}
|
||||
|
||||
if (wordCount >= maxWordCount) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (c == ' ' || c == '\t') {
|
||||
continue; // Leerzeichen und Tabs überspringen
|
||||
}
|
||||
else if (charCount < MAX_WORD_LEN - 1) {
|
||||
words[wordCount][charCount++] = (char)toupper((unsigned char)c);
|
||||
}
|
||||
}
|
||||
|
||||
if (charCount > 0 && wordCount < maxWordCount) {
|
||||
words[wordCount][charCount] = '\0';
|
||||
wordCount++;
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
|
||||
return wordCount;
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user