generated from freudenreichan/info2Praktikum-Wortsalat
90 lines
2.5 KiB
C
90 lines
2.5 KiB
C
#include "input.h"
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
#define MAX_NUMBER_OF_WORDS 100
|
|
#define MAX_WORD_LEN 100
|
|
// 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 line [MAX_WORD_LEN];
|
|
printf("H");
|
|
|
|
int j = 0;
|
|
int count = 0;
|
|
char single_word[MAX_WORD_LEN];
|
|
|
|
while(fgets(line, MAX_WORD_LEN, file) != NULL ){
|
|
|
|
printf("A");
|
|
for(int i = 0; line[i] != '\0'; i++){
|
|
printf("L");
|
|
|
|
|
|
//prüft, ob das gesammelte Zeichen ein Zeichen ist, schiebt es dann Großgeschrieben in das Array word
|
|
if (isalpha(line[i]) != 0){
|
|
single_word[j++] = toupper(line[i]);
|
|
}
|
|
// wenn das Wort fertig ist, dann wird dem Wort eine 0 angefügt um das Ende des Strings zu signalisieren, dann wird es in das Array words kopiert
|
|
else{
|
|
single_word[j] = '\0';
|
|
strncpy(words[count], single_word, MAX_WORD_LEN);
|
|
count++;
|
|
j = 0;
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
for(int n = 0; n < count ; n++)
|
|
{
|
|
printf("%s \n", words[n]);
|
|
printf("O");
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
int main(){
|
|
int wordCount = 0;
|
|
FILE *file = fopen("word.txt", "r");
|
|
char words[MAX_NUMBER_OF_WORDS][MAX_WORD_LEN];
|
|
wordCount = readWords(file, words, MAX_NUMBER_OF_WORDS);
|
|
fclose(file);
|
|
return wordCount;
|
|
}
|
|
|
|
/* 1. Versuch
|
|
while(fgets(line, maxWordCount, file) != NULL ){
|
|
|
|
int i = 0;
|
|
int count = 0;
|
|
char line [MAX_WORD_LEN];
|
|
|
|
|
|
while(line[i] != '\0' && line[i+1] != '\0'){
|
|
|
|
char single_word[MAX_WORD_LEN];
|
|
|
|
//prüft, ob das gesammelte Zeichen ein Zeichen ist, schiebt es dann Großgeschrieben in das Array word
|
|
if (isalpha(line[i]) != 0){
|
|
single_word[i++] = toupper(line[i]);
|
|
|
|
}
|
|
// wenn das Wort fertig ist, dann wird dem Wort eine 0 angefügt um das Ende des Strings zu signalisieren, dann wird es in das Array words kopiert
|
|
else{
|
|
single_word[i++] = '\0';
|
|
strncpy(single_word, words, i)
|
|
words[i][0] = '\0';
|
|
}
|
|
i++;
|
|
|
|
}
|
|
|
|
*/
|
|
|