forked from freudenreichan/info2Praktikum-Wortsalat
Implement readWords
This commit is contained in:
1 parent
96082d1781
commit
ae1adadfee
1 file changed
+37
-4
+37
-4
@@ -5,8 +5,41 @@
|
|||||||
// 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'
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
int readWords(FILE *file, char words[][MAX_WORD_LEN],
|
||||||
|
unsigned int maxWordCount)
|
||||||
|
{
|
||||||
|
if (file == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char line[MAX_LINE_LEN];
|
||||||
|
unsigned int wordCount = 0;
|
||||||
|
|
||||||
|
while (wordCount < maxWordCount &&
|
||||||
|
fgets(line, sizeof(line), file) != NULL) {
|
||||||
|
|
||||||
|
char *word = strtok(line, " ,;\n\t\r");
|
||||||
|
|
||||||
|
while (word != NULL && wordCount < maxWordCount) {
|
||||||
|
strncpy(words[wordCount], word, MAX_WORD_LEN - 1);
|
||||||
|
words[wordCount][MAX_WORD_LEN - 1] = '\0';
|
||||||
|
|
||||||
|
unsigned int i = 0;
|
||||||
|
|
||||||
|
while (words[wordCount][i] != '\0') {
|
||||||
|
words[wordCount][i] =
|
||||||
|
(char)toupper((unsigned char)words[wordCount][i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
wordCount++;
|
||||||
|
|
||||||
|
word = strtok(NULL, " ,;\n\t\r");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int)wordCount;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user