43 lines
1.2 KiB
C

#include "input.h"
#include <string.h>
#include <ctype.h>
// Nimmt einen String und schreibt dessen Characters in Großbuchstaben
void capitalletters(char *str){
while (*str) {
*str = toupper((unsigned char) *str);
str++;
}
}
// Liest Wörter aus Datei und speichert sie sicher im Array words
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
{
char buffer[MAX_WORD_LEN]; // temporärer Speicher für das aktuell bearbeitete Wort aus der Datei
unsigned int count_word = 0;
while (count_word < maxWordCount && fgets(buffer, sizeof(buffer), file) != NULL)
{
// Zeilenumbruch manuell entfernen
for (int i = 0; i < MAX_WORD_LEN; i++)
{
if (buffer[i] == '\n' || buffer[i] == '\r')
{
buffer[i] = '\0';
break;
}
}
// Alle Characters des Wort groß schreiben
capitalletters(buffer);
// Sicheres Kopieren des aktuellen Worts in das Zielarray
strncpy(words[count_word], buffer, MAX_WORD_LEN - 1);
words[count_word][MAX_WORD_LEN - 1] = '\0'; // Nullterminierung sicherstellen
count_word++;
}
return count_word;
}