1
#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

BraVeHurt
  • 11
  • 2
  • 1
    The `print()` function would invoke undefined behaviour if the file didn't exist and `entertext()` returned 0. Why bother returning an error code when you're not going to check for it? And do we really need 3 stars here? – Harith May 15 '23 at 18:07
  • 2
    Handful of scary things here. scary function definitions: `int correcttext(char ***x);` and `int entertext(char ***x);`. scary allocation: `while (fscanf(file, "%49s", dictWord = malloc(50 * sizeof(char))) != EOF) {`. – Jason May 15 '23 at 18:08
  • `fscanf()` would be passed a `NULL` pointer if the call to `malloc()` had failed. If realloc() returns NULL, you'd lose access to the previous memory and leak it. Use a temporary pointer. – Harith May 15 '23 at 18:08
  • safer: `dictWord[50];` followed by `fscanf(file, "%49s", dictWord)`, then no need to `free`. – Jason May 15 '23 at 18:10
  • `countwords()` is misleading. It doesn't count words at all. It simply prints a line of text. – Harith May 15 '23 at 18:11
  • The dictionary file is probably longer than the text file, leading to out of bounds in array in function 2, might be something to check. – Motomotes May 15 '23 at 18:16
  • 2
    Aside: please see [Triple pointers in C: is it a matter of style?](https://stackoverflow.com/questions/21488544/triple-pointers-in-c-is-it-a-matter-of-style) Answers say *"Using triple+ pointers is harming both readability and maintainability"*, and also *"Most of the time, the use of 3 levels of indirection is a symptom of bad design decisions made elsewhere in the program."* – Weather Vane May 15 '23 at 18:22
  • the project needs to be executed with dynamic memory allocation and not with arrays – BraVeHurt May 15 '23 at 18:24
  • @Motomotes you were right that was the problem... how can i solve it? – BraVeHurt May 15 '23 at 18:30

0 Answers0