Implemented parsing on delimiters in input.c
This commit is contained in:
1 parent
a290fba480
commit
51e9f406fd
1 file changed
+32
-13
+32
-13
@@ -1,6 +1,19 @@
|
||||
#include "input.h"
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#define DELIM_COUNT 4
|
||||
|
||||
int find_delim(const char* line, char delim_list[DELIM_COUNT]){
|
||||
int i = 0;
|
||||
while(line[i] != '\n'){
|
||||
for(int j = 0; j < DELIM_COUNT; j++){
|
||||
if(line[i] == delim_list[j])
|
||||
return i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
||||
@@ -10,25 +23,31 @@ int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
||||
{
|
||||
char line[MAX_LINE_LEN];
|
||||
int count = 0;
|
||||
|
||||
int word_end = 0;
|
||||
int word_start = 0;
|
||||
char delim_list[DELIM_COUNT] = {'\n', ';', ',', ' '};
|
||||
while (fgets(line, MAX_LINE_LEN, file) != NULL &&
|
||||
count < maxWordCount)
|
||||
{
|
||||
// Zeilenumbruch entfernen
|
||||
line[strcspn(line, "\n")] = '\0';
|
||||
word_end = 0;
|
||||
word_start = 0;
|
||||
while(line[word_start ? word_start - 1 : 0] != '\n' && count < maxWordCount){
|
||||
word_end = find_delim(line + word_start, delim_list);
|
||||
// Wort ins Array kopieren
|
||||
if(word_end) {
|
||||
strncpy(words[count], line + word_start, word_end);
|
||||
words[count][word_end] = '\0';
|
||||
|
||||
// Wort ins Array kopieren
|
||||
strncpy(words[count], line, MAX_WORD_LEN - 1);
|
||||
words[count][MAX_WORD_LEN - 1] = '\0';
|
||||
|
||||
// In Großbuchstaben umwandeln
|
||||
for (int i = 0; words[count][i] != '\0'; i++)
|
||||
{
|
||||
words[count][i] = toupper((unsigned char)words[count][i]);
|
||||
// In Großbuchstaben umwandeln
|
||||
for (int i = 0; words[count][i] != '\0'; i++)
|
||||
{
|
||||
words[count][i] = toupper((unsigned char)words[count][i]);
|
||||
}
|
||||
count++;
|
||||
}
|
||||
word_start += word_end + 1;
|
||||
}
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
Reference in new issue
Block a user