Finished input.c, should be FINISHED

This commit is contained in:
Lukas Weber 2025-11-04 20:27:48 +01:00
parent f036995e78
commit 32f9d2c965

View File

@ -3,20 +3,22 @@
#include <ctype.h> #include <ctype.h>
void readWordsFromLine(char buffer[], int* wordCount, char words[][MAX_WORD_LEN], unsigned int maxWords) { void readWordsFromLine(char buffer[], int* wordCount, char words[][MAX_WORD_LEN], unsigned int maxWords) {
char *token; char *token;
char *teiler = " ,;"; char *word_seperator = " ,;";
token = strtok(buffer, teiler); token = strtok(buffer, word_seperator);
while (*wordCount < maxWords && token != NULL) { //condition for reading new words from buffer while (*wordCount < maxWords && token != NULL) { //condition for reading new words from buffer
if (strlen(token) <= MAX_WORD_LEN) { //checks if word fits into buffer if (strlen(token) <= MAX_WORD_LEN) { //checks if word fits into wordsalad grid
for (int i = 0; i < strlen(token); i++) { for (int i = 0; i < strlen(token); i++) { //writes words char by char into array, stops at '\n'
if (token[i] == '\n') if (token[i] == '\n') {
break; break;
} else if (token[i] >= 97 && token[i] <= 122) { //converts lower case to upper case
token[i] = token[i] - 32;
}
words[*wordCount][i] = token[i]; words[*wordCount][i] = token[i];
} }
(*wordCount)++; (*wordCount)++;
} }
printf("%s\n", token); token = strtok(NULL, word_seperator);
token = strtok(NULL, teiler);
} }
} }
@ -29,11 +31,11 @@ void readWordsFromLine(char buffer[], int* wordCount,char words[][MAX_WORD_LEN],
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_LINE_LEN]; // prepares a large enough buffer to read one line from the .txt-file char buffer[MAX_LINE_LEN]; // prepares a large enough buffer to read one line from the .txt-file
int* wordCount; int readwords = 0;
*wordCount = 0; int *wordCount = &readwords;
while (*wordCount < maxWordCount && fgets(buffer, MAX_LINE_LEN, file) != NULL) { while (*wordCount < maxWordCount && fgets(buffer, MAX_LINE_LEN, file) != NULL) {
readWordsFromLine(buffer, *wordCount, words, maxWordCount); readWordsFromLine(buffer, wordCount, words, maxWordCount);
} }
printf("%d\n", readwords);
return 0; return *wordCount;
} }