forked from freudenreichan/info2Praktikum-Wortsalat
Merge branch 'main' of https://git.efi.th-nuernberg.de/gitea/thumannma105082/01_info2Praktikum-Wortsalat
This commit is contained in:
commit
46ba65eaa5
1 file changed
+49
-3
+49
-3
@@ -2,11 +2,57 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
// TODO:
|
// Trennzeichen zwischen Woertern (inkl. \r fuer Windows-Zeilenenden)
|
||||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
#define DELIMITERS " ,;\t\r\n"
|
||||||
|
|
||||||
|
// Kopiert ein Wort sicher nach 'dest' (max. destSize - 1 Zeichen) und wandelt es in Grossbuchstaben um.
|
||||||
|
// Gibt die Laenge des gespeicherten Wortes zurueck.
|
||||||
|
static size_t copyWordUpper(char *dest, const char *src, size_t destSize)
|
||||||
|
{
|
||||||
|
size_t len = 0;
|
||||||
|
|
||||||
|
if(destSize == 0)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
strncpy(dest, src, destSize - 1);
|
||||||
|
dest[destSize - 1] = '\0';
|
||||||
|
|
||||||
|
for(len = 0; dest[len] != '\0'; len++)
|
||||||
|
{
|
||||||
|
dest[len] = (char)toupper((unsigned char)dest[len]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
// Read words from file and store in 'words' array
|
// Read words from file and store in 'words' array
|
||||||
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||||
{
|
{
|
||||||
|
char line[MAX_LINE_LEN];
|
||||||
|
unsigned int wordCount = 0;
|
||||||
|
|
||||||
}
|
if(file == NULL || words == NULL)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Datei zeilenweise lesen, bis sie zu Ende ist oder das Array voll ist
|
||||||
|
while(wordCount < maxWordCount && fgets(line, sizeof(line), file) != NULL)
|
||||||
|
{
|
||||||
|
// Zeile an den Trennzeichen zerlegen
|
||||||
|
char *token = strtok(line, DELIMITERS);
|
||||||
|
|
||||||
|
while(token != NULL && wordCount < maxWordCount)
|
||||||
|
{
|
||||||
|
if(copyWordUpper(words[wordCount], token, MAX_WORD_LEN) > 0)
|
||||||
|
{
|
||||||
|
wordCount++;
|
||||||
|
}
|
||||||
|
token = strtok(NULL, DELIMITERS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int)wordCount;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user