Complementary task for topic: 7

M Nemeth 2023-08-29 15:21:04.626219'

DStrings+Struct: Simple Dictionary: menu.

DStrings+Struct: Simple Dictionary: menu.

Main goal:
Create a C program that simulates a simple dictionary application. The program should allow the user to add new words and their meanings to the dictionary and search for the meanings of existing words. The word and its meaning will be stored as dynamic strings in a dynamic structure using dynamic memory allocation.
Final task:
> Create menu driven program. The user can choose with the help of the keyboard what to do:
-add word
-search
-exit
On exit make sure all the dynamic memory is deallocated

Hint: Menu driven program usually looks like:
while (1) {
printf(options)...
scanf("%d", &choice); //This can be char
switch (choice) {
case 1:...break;
case 2:...brak;...
}
Make sure on exiting that the Dstrings are deallocated earlier than the array that contains it, otherwise you cannot reach the Dstrings.

Solution
#include 
#include 
#include 

typedef struct {
    char* word;
    char* meaning;
} DictionaryEntry;

void clearInputBuffer() {
    int c;
    while ((c = getchar()) != '\n' && c != EOF) {}
}

void addWord(DictionaryEntry** dictionary, int* wordCount) {
    DictionaryEntry newWord;

    printf("Enter the word: ");

    //scanf("%s", &newWord.word); Will not work! try out!
    //Dynamic read is complicated
    char c=getchar();
    int size=1;
    char* str=(char*)malloc(size*sizeof(char));
    str[0]='\0';
    while(c!='\n'){

        str=(char*)realloc(str,++size*sizeof(char));
        str[size-2]=c;
        str[size-1]='\0';
        c=getchar();
    }
    newWord.word=str;

    printf("Enter the meaning: ");
    //scanf("%s", &newWord.meaning); Will not work! try out!
    c=getchar();
    size=1;
    str=(char*)malloc(size*sizeof(char));
    str[0]='\0';
    while(c!='\n'){

        str=(char*)realloc(str,++size*sizeof(char));
        str[size-2]=c;
        str[size-1]='\0';
        c=getchar();
    }
    newWord.meaning=str;

    (*wordCount)++;
    *dictionary = (DictionaryEntry*)realloc(*dictionary, (*wordCount) * sizeof(DictionaryEntry));
    (*dictionary)[*wordCount - 1] = newWord;

    printf("Word added successfully!\n");
}

void searchMeaning(DictionaryEntry* dictionary, int wordCount, char* searchWord) {
    int found = 0;
    for (int i = 0; i < wordCount; i++) {
        if (strcmp(dictionary[i].word, searchWord) == 0) {
            printf("Meaning: %s\n", dictionary[i].meaning);
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("Word %s not found in the dictionary.\n",searchWord);
    }
}


int main() {
    int wordCount = 0;
    DictionaryEntry* dictionary = NULL;
    int choice;
    char searchWord[100];

    printf("--- Dictionary Application ---\n");
    while (1) {
        printf("1. Add new word\n");
        printf("2. Search for meaning\n");
        printf("3. Exit\n");
        printf("\nEnter your choice: ");
        scanf("%d", &choice);
        clearInputBuffer();
        switch (choice) {
            case 1:
                addWord(&dictionary, &wordCount);
                break;
            case 2:
                printf("Enter the word to search: ");
                scanf("%99s", searchWord);
                clearInputBuffer();
                searchMeaning(dictionary, wordCount, searchWord);
                break;
            case 3:
                printf("Exiting... Goodbye!\n");

                // Deallocate the memory used by the dictionary
                for (int i = 0; i < wordCount; i++) {
                    free(dictionary[i].word);
                    free(dictionary[i].meaning);
                }
                free(dictionary);

                return 0;
            default:
                printf("Invalid choice. Try again.\n");
        }
    }

    return 0;
}



Explanation
    The program defines a DictionaryEntry struct that holds two dynamic strings: word and meaning.

    The clearInputBuffer() function is used to clear any remaining characters in the input buffer after using scanf().

    The addWord() function is responsible for adding a new word with its meaning to the dictionary. It reads the string character-bycharacter and allocates the memory for the dynamic strings (word and meaning) entered by the user.

    The searchMeaning() function searches for the meaning of an existing word in the dictionary. If the word is found, it prints the meaning; otherwise, it displays a message indicating that the word was not found.

    In the main() function, the user is prompted to choose from three options: add a new word, search for the meaning of a word, or exit the program.

    For each option, appropriate functions are called, and the user is provided with feedback messages.

    The program uses dynamic memory allocation to create and manage the dictionary. Memory is allocated as needed for each word and its meaning.

    When the user chooses to exit the program, the memory used by the dynamic strings and the dictionary is deallocated using free() to avoid memory leaks.
< < previous    next > >