forked from freudenreichan/info2Praktikum-Wortsalat
Bearbeitung von input.c
This commit is contained in:
1 parent
93db739464
commit
20496fdfba
1 file changed
+45
-1
+45
-1
@@ -3,10 +3,54 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
// 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
|
// 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)
|
||||||
{
|
{
|
||||||
|
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