#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WORD_SIZE 20
#define MEANING_SIZE 50
#define TEMP_SIZE 200
//defining structure
struct dictionary
{
    char word[WORD_SIZE];
    char meaning[MEANING_SIZE];
    struct dictionary *right;
    struct dictionary *left;
};
struct dictionary* Min(struct dictionary* Tdic)
{
    if (Tdic == NULL)
        //empty tree
        return NULL;
    else if (Tdic->left == NULL)
        return(Tdic);
    else
        //find min recursive
        return(Min(Tdic->left));
}
//insertion operation for node in the tree
struct dictionary* insertToTree(char word[],char meaning[],struct dictionary *Tdic)
{
    if(Tdic==NULL)
    {
        //malloc a new node
        Tdic = (struct dictionary*)malloc(sizeof(struct dictionary));
        if(Tdic==NULL)
            printf("Out of Space\n");
        else
        {
            //fill the node
            strcpy(Tdic->word,word);
            strcpy(Tdic->meaning,meaning);
            Tdic->left = Tdic->right = NULL;
        }
    }
    //go to the left of a tree
    else if(strcmp(word,Tdic->word)<0)
        Tdic->left = insertToTree(word,meaning,Tdic->left);
    //go to the right of a tree
    else if (strcmp(word,Tdic->word)>0)
        Tdic->right = insertToTree(word,meaning,Tdic->right);
    return Tdic;
}
//delete all the spaces,commas,numbers in a string
void trim(char *s)
{
    int counter = 0;
    //counter for the number of spaces at the begini of a string
    while(s[counter]==' ')
        counter++;
    if(counter!=0)
    {
        int i = 0;
        while(s[i+counter]!='\0')
        {
            //shifting elements in the array
            s[i] = s[i+counter];
            i++;
        }
        //add /0 to the end of the string
        if(s[i-1] == '\n')
            s[i-1] = '\0';
        else
            s[i] = '\0';
    }
}
//reading information from file
struct dictionary* read_file(struct dictionary *Tdic)
{
    FILE*input = fopen("dictionary.txt","r");
    //check if the file is empty or not
    if(input==NULL)
    {
        printf("Empty File!\n");
        exit(1);
    }
    char temp[TEMP_SIZE];
    char *token;
    char word[WORD_SIZE];
    char meaning[MEANING_SIZE];
    //reading each line and store it in temp
    while(fgets(temp,sizeof(temp),input))
    {
        //check if the line in the file is empty or not
        if(temp[0]=='\n')
            //if it's empty skip this line and read the next line
            continue;
        //toknize temp till the dot
        token = strtok(temp,".");
        while(token!=NULL)
        {
            //toknize temp from NULL till :
            token = strtok(NULL,":");
            //token points to the word
            strcpy(word,token);
            //trim the word
            trim(word);
            word[strlen(word)+1] = '\0';
            //toknize temp till \t
            token = strtok(NULL,"\t");
            //token will point to the meaning of the word above
            strcpy(meaning,token);
            //trim the meaning
            trim(meaning);
            token = strtok(NULL,".");
            //add the word and it's meaning to the AVL tree
            Tdic = insertToTree(word,meaning,Tdic);
        }
    }
    fclose(input);
    return Tdic;
}
//select the number of operation by the user
int list()
{
    int click;
    printf("Choose one of the following:");
    printf("\n\n1)Search for a word and display its meaning\n");
    printf("2)Update the meaning of an existing word\n");
    printf("3)Insert a new word from the user with its meaning\n");
    printf("4)Print all words in the dictionary in an alphabetic order with their associated meanings\n");
    printf("5)Print all words that start with a specific character in an alphabetic order\n");
    printf("6)Delete a word from the dictionary\n");
    printf("7)Delete all words that start with a specific letter\n");
    printf("8)Save all words back in file dictionary.txt\n");
    printf("9)Exit\n");
    printf("----------------------------------------------------------------------------------------------\n");
    scanf("%d",&click);
    //check if the operation is available or not
    if(click > 9 || click < 1)
    {
        printf("Error!\nPlease try again\n\n");
        click = list();
    }
    return click;
}
//find a node that contain a specific word from the tree
struct dictionary* Find(char word[],struct dictionary *Tdic)
{
    struct dictionary *temp;
    temp = Tdic;
    //if empty tree or the word does not exist return NULL
    if (temp == NULL)
        return temp;
    else if (strcmp(word,temp->word)>0)
        return Find(word,temp->right);
    else if (strcmp(word,temp->word)<0)
        return (Find(word,temp->left));
    else
        return temp;
}
//search for a word and display it's meaning
void searchAndDisplay(struct dictionary *Tdic)
{
    char word[WORD_SIZE];
    struct dictionary *temp;
    printf("Please enter the word that you are searching for: \n");
    scanf("%s",word);
    temp = Find(word,Tdic);
    if(temp==NULL)
        printf("The tree does not contain the the word (%s)\n\n",word);
    else
        printf("%s : %s\n\n",word,temp->meaning);
}
//update the meaning of a specific word
void updateMeaning(struct dictionary *Tdic)
{
    char word[WORD_SIZE];
    char meaning[MEANING_SIZE];
    struct dictionary *temp = Tdic;
    printf("Please enter the word that you want to update: \n");
    scanf("%s",word);
    temp = Find(word,Tdic);
    if(temp==NULL)
        printf("The tree does not contain the the word (%s)\n\n",word);
    else
    {
        //enter the word and update it's menaing
        printf("Please enter the meaning: \n");
        scanf("%s",meaning);
        strcpy(temp->meaning,meaning);
        printf("(%s) meaning is updated successfully\n\n",word);
    }
}
//add word to the tree and it's meaning
void insertWord(struct dictionary *Tdic)
{
    char word[WORD_SIZE];
    char meaning[MEANING_SIZE];
    printf("Please enter the word that you want to insert: \n");
    scanf("%s",word);
    printf("Please enter it's meaning\n");
    scanf("%s",meaning);
    //add the word with it's meaning to the tree
    Tdic = insertToTree(word,meaning,Tdic);
    printf("(%s) inserted successfully to the tree\n\n",word);
}
//print all words in the tree with there meanings in alphabetic order
void printWordsMeaning(struct dictionary *Tdic)
{
    //inOrder print
    struct dictionary *temp = Tdic;
    if(temp!=NULL)
    {
        printWordsMeaning(temp->left);
        printf("%s",temp->word);
        printf(": %-20s\n",temp->meaning);
        printWordsMeaning(temp->right);
    }
}
//print all words in the tree that start with spcefic character in alphabetic order
void printAlphabet(char character[],struct dictionary *Tdic)
{
    if(Tdic==NULL)
        return;
    printAlphabet(character,Tdic->left);
    if(strncasecmp(Tdic->word,character,1)==0)
    {
        printf("%s",Tdic->word);
        printf(" : %s\n",Tdic->meaning);
    }
    printAlphabet(character,Tdic->right);
}
//delete word from the tree
struct dictionary* deleteWord(char word[],struct dictionary *Tdic)
{
    struct dictionary *temp;
    if(Tdic==NULL)
        printf("(%s) not found in the file\n\n",word);
    else if(strcmp(word,Tdic->word)<0)
        Tdic->left = deleteWord(word,Tdic->left);
    else if(strcmp(word,Tdic->word)>0)
        Tdic->right = deleteWord(word,Tdic->right);
    else if(Tdic->left && Tdic->right)
    {
        temp = Min(Tdic->right);
        strcpy(Tdic->word,temp->word);
        Tdic->right = deleteWord(word,Tdic->right);
    }
    else
    {
        temp = Tdic;
        if(Tdic->left == NULL)
            Tdic = Tdic->right;
        else if(Tdic->right == NULL)
            Tdic = Tdic->left;
        printf("Deletion done successfully\n\n");
        free(temp);
    }
    return Tdic;
}
//delete all words in the tree that start with spcefic character
struct dictionary* deleteWithAlphabet(struct dictionary *Tdic,char word[])
{
    struct dictionary *temp = Tdic;
    if(temp!=NULL)
    {
        temp->left = deleteWithAlphabet(temp->left,word);
        temp->right = deleteWithAlphabet(temp->right,word);
        if(strncmp(temp->word,word,1)==0)
        {
            temp = deleteWord(temp->word,Tdic);
        }
    }
    return temp;
}
//save and print the tree in a file
void saveInFile(struct dictionary *Tdic,FILE *output)
{
    if(Tdic!=NULL)
    {
        saveInFile(Tdic->left,output);
        fprintf(output,"%s",Tdic->word);
        fprintf(output," :%s\n",Tdic->meaning);
        saveInFile(Tdic->right,output);
    }
}
int main()
{
    //integer for the number of operation that the user want
    int click;
    char word[WORD_SIZE];
    struct dictionary* Tdic = NULL;
    //reading the file function
    Tdic = read_file(Tdic);
    //display the list
    click = list();
    while(click!=9)
    {
        switch(click)
        {
            case 1:searchAndDisplay(Tdic);
                break;
            case 2:updateMeaning(Tdic);
                break;
            case 3:insertWord(Tdic);
                break;
            case 4:printWordsMeaning(Tdic);
                    printf("\n");
                    break;
            case 5:{printf("Please enter the character: \n");
                    scanf("%s",word);
                    printAlphabet(word,Tdic);
                    printf("\n");
                break;}
            case 6:{printf("Please enter the word you want to delete:\n");
                    scanf("%s",word);
                    Tdic = deleteWord(word,Tdic);
                break;}
            case 7:{printf("Please enter a character: \n");
                    scanf("%s",word);
                    Tdic = deleteWithAlphabet(Tdic,word);
                break;}
            case 8:{FILE*output = fopen("dictionary.txt","w");
                    saveInFile(Tdic,output);
                    fclose(output);
                    printf("The information has been saved to the file successfully\n\n");
                break;}
        }
        //call the list again
        click = list();
    }
    printf("The program ended\n");
    return 0;
} 

C Language online compiler

Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!

Read inputs from stdin

OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.

#include <stdio.h>
int main()
{
    char name[50];
    printf("Enter name:");
    scanf("%s", name);
    printf("Hello %s \n" , name );
    return 0;
    
}

About C

C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.

Key features:

  • Structured Programming
  • Popular system programming language
  • UNIX, MySQL and Oracle are completely written in C.
  • Supports variety of platforms
  • Efficient and also handle low-level activities.
  • As fast as assembly language and hence used as system development language.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition if-else is used.

if(conditional-expression) {
   // code
} else {
   // code
}

You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to if-else-if ladder.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Arrays

Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.

Syntax

One dimentional Array:

data-type array-name[size];

Two dimensional array:

data-type array-name[size][size];

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

Two types of functions are present in C

  1. Library Functions:

Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,

  1. User defined functions:

User defined functions are the ones which are written by the programmer based on the requirement.

How to declare a Function

return_type function_name(parameters);

How to call a Function

function_name (parameters)

How to define a Function

return_type function_name(parameters) {  
  //code
}