forked from freudenreichan/info2Praktikum-Wortsalat
Merge branch 'game'
This commit is contained in:
commit
84e854fdc2
35 files changed
+11
No files matched your search
@@ -0,0 +1,56 @@
|
||||
#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)
|
||||
{
|
||||
int wordCount = 0;
|
||||
char line[MAX_LINE_LEN];
|
||||
|
||||
while (fgets(line, MAX_LINE_LEN, file)) {
|
||||
char currentWord[MAX_LINE_LEN];
|
||||
int wordPos = 0;
|
||||
|
||||
for ( int i = 0; line[i] != '\0'; i++) {
|
||||
if (line[i] == ',' || line[i] == ';' || line[i] == ' ' || line[i] == '\n') {
|
||||
|
||||
if (wordPos > 0) {
|
||||
|
||||
currentWord[wordPos] = '\0';
|
||||
|
||||
if (wordCount < maxWordCount) {
|
||||
|
||||
strcpy(words[wordCount], currentWord);
|
||||
wordCount++;
|
||||
}
|
||||
wordPos = 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (wordPos < MAX_WORD_LEN-1) {
|
||||
|
||||
currentWord[wordPos] = toupper(line[i]);
|
||||
wordPos++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (wordPos > 0) {
|
||||
|
||||
currentWord[wordPos] = '\0';
|
||||
|
||||
if (wordCount < maxWordCount) {
|
||||
|
||||
strcpy(words[wordCount], currentWord);
|
||||
wordCount++;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return wordCount;
|
||||
}
|
||||
Reference in new issue
Block a user