fertig 2.0
This commit is contained in:
1 parent
6259f06391
commit
4e556279e5
9 files changed
+141
-94
No files matched your search
+36
-14
@@ -2,24 +2,46 @@
|
||||
#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.
|
||||
// Trennzeichen, die in words.txt zwischen den Wörtern vorkommen
|
||||
static int isDelimiter(int c)
|
||||
{
|
||||
return c == ',' || c == ';' || isspace((unsigned char)c);
|
||||
}
|
||||
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
unsigned int wordCount = 0;
|
||||
char buffer[MAX_WORD_LEN];
|
||||
|
||||
while (wordCount < maxWordCount && fscanf(file, "%s", buffer) == 1) {
|
||||
|
||||
for (int i = 0; buffer[i]; i++) {
|
||||
buffer[i] = tolower(buffer[i]);
|
||||
}
|
||||
strncpy(words[wordCount], buffer, MAX_WORD_LEN - 1);
|
||||
words[wordCount][MAX_WORD_LEN - 1] = '\0';
|
||||
wordCount++;
|
||||
if (file == NULL || words == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return wordCount;
|
||||
unsigned int count = 0;
|
||||
unsigned int len = 0;
|
||||
int c;
|
||||
|
||||
while (count < maxWordCount && (c = fgetc(file)) != EOF)
|
||||
{
|
||||
if (isDelimiter(c))
|
||||
{
|
||||
if (len > 0)
|
||||
{
|
||||
words[count][len] = '\0';
|
||||
count++;
|
||||
len = 0;
|
||||
}
|
||||
}
|
||||
else if (len < MAX_WORD_LEN - 1)
|
||||
{
|
||||
words[count][len++] = (char)toupper((unsigned char)c);
|
||||
}
|
||||
}
|
||||
|
||||
if (len > 0 && count < maxWordCount)
|
||||
{
|
||||
words[count][len] = '\0';
|
||||
count++;
|
||||
}
|
||||
|
||||
return (int)count;
|
||||
}
|
||||
Reference in new issue
Block a user