#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getchoice();
int print(char **x, int num);
int entertext(char ***x);
int correcttext(char ***x);// Τυπώνει μόνο τις λέξεις του κειμένου που υπάρχουν μέσα στο λεξιλόγιο
int countwords(char **x);
int countletters(char **x);
int main() {
char **text = NULL;
int choice=0;
int num=0;
while ((choice = getchoice()) != -1) {
switch (choice) {
case 1:
num = entertext(&text);
print(text, num);
break;
case 2:
correcttext(&text);
break;
case 3:
countwords(text);
break;
case 4:
countletters(text);
break;
default:
break;
}
}
for (int i = 0; i < num; i++) {
free(text[i]);
}
free(text);
return 0;
}
int getchoice(void) {
int choice;
printf("\n1: Enter text\n2: Correct text\n3: Number of words\n4: Number of letters\n-1: Exit\n>");
while (scanf("%d", &choice) != 1) {
printf("No such option available... please enter a number between 1 and 4, or -1 to exit: ");
while (getchar() != '\n');
}
if (choice < -1 || choice > 4) {
printf("No such option available... please enter a number between 1 and 4, or -1 to exit: ");
exit(1);
}
return choice;
}
int print(char **x, int num) {
for (int i = 0; i < num; i++) {
printf(" %s\n", x[i]);
}
return 0;
}
int entertext(char ***x) {
int size = 0;
*x = NULL;
char *word = NULL;
FILE *file;
file = fopen("coffee.txt", "r");
if (file == NULL) {
printf("\nError opening file\n");
return 0;
}
while (fscanf(file, "%49s", word = malloc(50 * sizeof(char))) != EOF) {
size++;
*x = realloc(*x, size * sizeof(char *));
(*x)[size - 1] = malloc((strlen(word) + 1) * sizeof(char));
strcpy((*x)[size - 1], word);
free(word); // Free the memory allocated for word after it has been copied to (*x)
}
fclose(file);
return size;
}
int correcttext(char ***x) {
int size = 0;
char **dict = NULL;
char *dictWord = NULL; // Rename the variable to avoid conflict
FILE *file;
file = fopen("englishWords.txt", "r");
if (file == NULL) {
printf("\nError opening file\n");
return 0;
}
while (fscanf(file, "%49s", dictWord = malloc(50 * sizeof(char))) != EOF) {
size++;
dict = realloc(dict, size * sizeof(char *));
dict[size - 1] = malloc((strlen(dictWord) + 1) * sizeof(char));
strcpy(dict[size - 1], dictWord);
free(dictWord); // Free the memory allocated for dictWord after it has been copied to dict
}
fclose(file);
for (int i = 0; i < size; i++) {
for (int j = 0; (*x)[i][j] != '\0'; j++) {
char word[30] = "";
int k = 0;
while ((*x)[i][j] != ' ' && (*x)[i][j] != '\t' && (*x)[i][j] != '\n' && (*x)[i][j] != '\0') {
word[k++] = (*x)[i][j++];
}
word[k] = '\0';
for (int m = 0; m < size; m++) {
if (strcmp(word, dict[m]) == 0) {
printf("%s\n", (*x)[i]);
break;
}
}
if ((*x)[i][j] == '\0') {
break;
}
}
}
for (int i = 0; i < size; i++) {
free(dict[i]);
}
free(dict);
return 0;
}
int countwords(char **x) {
printf("countwords, %s", x[0]);
return 0;
}
int countletters(char **x) {
printf("countletters, %s", x[0]);
return 0;
}
first you type 1 to load the first .txt file and then 2 to load the dictionary file and compare them, but after typing 2 in order for the correcttext function to run the programm does what the correcttext function is supposed to do but instead of returning to the menu it stops for a second and then terminates