generated from freudenreichan/info2Praktikum-Wortsalat
139 lines
3.8 KiB
C
139 lines
3.8 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)
|
|
{
|
|
// MAX_WORD_LEN 100
|
|
// MAX_LINE_LEN 1024
|
|
// *file ist Datei
|
|
// words ist Array
|
|
// MAX_WORD_LEN ist maximale Wortlänge
|
|
// maxWordCount ist maximale Anzahl an Wörtern
|
|
char puffer[MAX_LINE_LEN];
|
|
int counter = 0;
|
|
|
|
while(fgets(puffer, MAX_LINE_LEN-1, file) != NULL && counter < maxWordCount)
|
|
{
|
|
char *parts = strtok(puffer, ",;\n\t/. ");
|
|
|
|
while(parts != NULL && counter < maxWordCount)
|
|
{
|
|
//strncpy(words[counter][MAX_WORD_LEN], puffer, MAX_LINE_LEN-1);
|
|
//words[counter][MAX_WORD_LEN-1] = "\0";
|
|
//Großbuchstaben
|
|
for(int i = 0; i < MAX_WORD_LEN -1 && parts[i] != '\0'; i++)
|
|
{
|
|
words[counter][i] = toupper(parts[i]);
|
|
words[counter][i] = '\0';
|
|
}
|
|
}
|
|
counter++; // Wort eingelesen -> Wortzähler erhöhen
|
|
parts = strtok(NULL, ",;\n\t/. ");
|
|
|
|
}
|
|
return counter;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* #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++;
|
|
|
|
}
|
|
|
|
*/
|
|
|