forked from freudenreichan/info2Praktikum-Wortsalat
neue Lösung von input.c
This commit is contained in:
1 parent
61193c20ab
commit
efd28dec07
1 file changed
+34
-47
+34
-47
@@ -2,57 +2,44 @@
|
||||
#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.
|
||||
int splitWords(char texRow[], int maxLength, int wordNumber, char words[][MAX_WORD_LEN], unsigned int maxWordCount);
|
||||
// Read words from file
|
||||
// Liest ein einzelnes Wort aus der Datei in 'buffer' (max. bufferSize Zeichen inkl. '\0')
|
||||
// Gibt 'buffer' zurück oder NULL, wenn kein Wort mehr vorhanden ist
|
||||
static char *readWord(FILE *file, char *buffer, unsigned int bufferSize)
|
||||
{
|
||||
unsigned int len = 0;
|
||||
int c = fgetc(file);
|
||||
|
||||
// Trennzeichen (Leerzeichen, Komma, Semikolon, Zeilenumbruch, ...) überspringen
|
||||
while (c != EOF && !isalpha(c))
|
||||
c = fgetc(file);
|
||||
|
||||
// Buchstaben sammeln, bis ein Trennzeichen oder das Dateiende kommt
|
||||
while (c != EOF && isalpha(c))
|
||||
{
|
||||
if (len < bufferSize - 1)
|
||||
buffer[len++] = toupper(c);
|
||||
c = fgetc(file);
|
||||
}
|
||||
|
||||
buffer[len] = '\0';
|
||||
|
||||
if (len == 0)
|
||||
return NULL;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
// Read words from file and store in 'words' array
|
||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
char word[MAX_WORD_LEN];
|
||||
unsigned int wordCount = 0;
|
||||
|
||||
// Datei Zeilenweise auslesen
|
||||
char textRow[MAX_LINE_LEN];
|
||||
int row = 0;
|
||||
|
||||
while (fgets(textRow, MAX_LINE_LEN, file) != NULL)
|
||||
row = splitWords(textRow, MAX_LINE_LEN, row, words, maxWordCount);
|
||||
|
||||
fclose(file);
|
||||
|
||||
for (int i; i < row; i++)
|
||||
printf("%s\n", words[i]);
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
// Zeilen in lesbare Wörter aufteilen und "words" zuweisen
|
||||
int splitWords(char textRow[], int maxStringLength, int row, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
char *teiler = " .,\n;";
|
||||
char *token = strtok(textRow, teiler);
|
||||
|
||||
if (token != NULL)
|
||||
while (wordCount < maxWordCount && readWord(file, word, MAX_WORD_LEN) != NULL)
|
||||
{
|
||||
if (row < maxWordCount)
|
||||
{
|
||||
strncpy(words[row], token, maxStringLength);
|
||||
words[row][maxStringLength - 1] = '\0';
|
||||
row++;
|
||||
}
|
||||
strncpy(words[wordCount], word, MAX_WORD_LEN - 1);
|
||||
words[wordCount][MAX_WORD_LEN - 1] = '\0';
|
||||
wordCount++;
|
||||
}
|
||||
|
||||
while (token != NULL && row < maxWordCount)
|
||||
{
|
||||
|
||||
token = strtok(NULL, teiler);
|
||||
|
||||
if (token != NULL)
|
||||
{
|
||||
strncpy(words[row], token, maxStringLength);
|
||||
words[row][maxStringLength - 1] = '\0';
|
||||
row++;
|
||||
}
|
||||
}
|
||||
|
||||
return row;
|
||||
}
|
||||
return wordCount;
|
||||
}
|
||||
Reference in new issue
Block a user