generated from freudenreichan/info2Praktikum-Wortsalat
56 lines
1.5 KiB
C
56 lines
1.5 KiB
C
#include "input.h"
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
// TODO:
|
|
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt.
|
|
|
|
|
|
// Read words from file and store in 'words' array
|
|
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)
|
|
{
|
|
|
|
char zeile[MAX_LINE_LEN];
|
|
char inhalt[MAX_WORD_LEN*maxWordCount];
|
|
const char *trenner = " .;,";
|
|
char *token = NULL;
|
|
const char *enter = "\n";
|
|
|
|
//Zeilen auslesen und als ein String speichern
|
|
while(fgets(zeile,MAX_LINE_LEN, file) != NULL){
|
|
if (strstr(zeile, enter) != NULL){
|
|
zeile[strlen(zeile)-1] = ';';
|
|
}
|
|
strncat(inhalt, zeile, strlen(zeile));
|
|
}
|
|
|
|
//erstes Wort finden
|
|
token = strtok(inhalt, trenner);
|
|
|
|
//Woerter auftrennen und in words[][] speichern
|
|
int counter = 0;
|
|
while(token != NULL){
|
|
strncpy(words[counter], token, MAX_WORD_LEN-1);
|
|
words[counter][MAX_WORD_LEN - 1] = '\0'; //Sicherstellen von Stringende
|
|
token = strtok(NULL, trenner);
|
|
counter ++;
|
|
}
|
|
inhalt[0]='\0';
|
|
|
|
for(int i=0;i<counter;i++){ //Schleife für nur Großbuchstaben
|
|
for(int ii=0;ii<strlen(words[i]);ii++){
|
|
words[i][ii] = toupper(words[i][ii]);
|
|
}
|
|
}
|
|
|
|
//Test
|
|
for(int i =0;i<counter;i++){
|
|
printf("%s\n", words[i]);
|
|
}
|
|
|
|
return counter;
|
|
}
|
|
|
|
|
|
|
|
//Wörter von lang nach kurz sortieren
|