generated from freudenreichan/info2Praktikum-Wortsalat
created basic functionality
This commit is contained in:
1 parent
bde98ef09f
commit
74c3415bae
1 file changed
+43
@@ -1,12 +1,55 @@
|
|||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <stdio.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
|
||||||
|
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 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 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; i++)
|
||||||
|
{
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user