Files
adrian_andi_wortsalat/Startcode/input.c
T
2026-10-26 09:13:47 +01:00

55 lines
1.4 KiB
C

#include "input.h"
#include <string.h>
#include <ctype.h>
#include <stdio.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
static int createSingleWords(char text[MAX_LINE_LEN], char words[][MAX_WORD_LEN], unsigned int maxWordCount);
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
int wordCount = 0;
int i = 0;
char lineRead[MAX_LINE_LEN];
while(fgets(lineRead, MAX_LINE_LEN, file) != NULL && i < maxWordCount)
{
wordCount += createSingleWords(lineRead, words, maxWordCount);
i++;
}
return i; //wordCount
}
static int createSingleWords(char text[MAX_LINE_LEN], char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
char *teiler = " ;,.\n";
char *token;
int i = 0;
token = strtok(text, teiler);
while(token != NULL && i < maxWordCount)
{
strncpy(words[i], token, MAX_WORD_LEN - 1);
//kleinbuchstaben --> GROßBUCHSTABEN
int length = strlen(words[i]);
for(int j = 0; j < length; j++)
{
if(islower(words[i][j]))
{
words[i][j] = toupper(words[i][j]);
}
}
words[i][MAX_WORD_LEN - 1] = '\0';
token = strtok(NULL, teiler);
i++;
}
return i;
}