generated from freudenreichan/info2Praktikum-Wortsalat
input neu geschrieben
This commit is contained in:
parent
f89019437f
commit
84de10d05b
@ -2,7 +2,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#define MAX_BUFFER_LEN 256
|
static char *buffer;
|
||||||
|
|
||||||
// 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.
|
||||||
@ -10,29 +10,51 @@
|
|||||||
// 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 buffer[MAX_BUFFER_LEN];
|
int i = 0;
|
||||||
int count = 0;
|
int word = 0;
|
||||||
char *teiler = ".,; !?\n";
|
int wordChar = 0;
|
||||||
|
int firstLetter = 0;
|
||||||
|
|
||||||
if (file == NULL)
|
if (file == NULL)
|
||||||
{
|
{
|
||||||
printf("Fehler beim Öffnen von words.txt!\n");
|
return 0;
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while(fgets(buffer, MAX_LINE_LEN, file))
|
fscanf(file, "%10s", buffer);
|
||||||
|
|
||||||
|
do
|
||||||
{
|
{
|
||||||
const char *wort = strtok(buffer, teiler);
|
|
||||||
|
|
||||||
while(wort != NULL && count < maxWordCount)
|
if ((buffer[i] >= 'A') && (buffer[i] <= 'Z'))
|
||||||
{
|
{
|
||||||
strncpy(words[count], wort, MAX_WORD_LEN-1);
|
if (!firstLetter)
|
||||||
words[count][MAX_WORD_LEN-1] = '\0';
|
{
|
||||||
count++;
|
firstLetter = 1;
|
||||||
|
|
||||||
wort = strtok(NULL, teiler);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return count;
|
words[word][wordChar] = buffer[i];
|
||||||
|
wordChar++;
|
||||||
|
}
|
||||||
|
else if ((buffer[i] >= 'a') && (buffer[i] <= 'z'))
|
||||||
|
{
|
||||||
|
if (!firstLetter)
|
||||||
|
{
|
||||||
|
firstLetter = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
words[word][wordChar] = toupper(buffer[i]);
|
||||||
|
wordChar++;
|
||||||
|
}
|
||||||
|
else if (firstLetter)
|
||||||
|
{
|
||||||
|
words[word][wordChar] = '\0';
|
||||||
|
word++;
|
||||||
|
wordChar = 0;
|
||||||
|
firstLetter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
} while ((i < MAX_LINE_LEN) && (word < maxWordCount));
|
||||||
|
|
||||||
|
return word+1;
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user