Delete first ocurence from a linked list

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1256 Views - Last Post: 07 July 2020 - 09:18 AM Rate Topic: -----

#1 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

Delete first ocurence from a linked list

Posted 03 July 2020 - 12:28 PM

after deleting suppose a word starting by the letter a this work should be removed from t[0] so far it works great only problem i'm having is if its the first element in the list for exemple arbre=arbre is the first and only element starting by a
if i try to delete arbre that's what i get on printing t[0]
inserer un mot :
arbre
@ arbre

any idea what i'm doing wrong here ?


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * File:   main.c
 * Author: USER
 *
 * Created on June 23, 2020, 7:51 PM
 */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


    
typedef struct cell {
    char mot[255];
    char signification[255];
    struct cell *suivant;
} cell;

char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    cell * t[26] = { 0 };

void print(cell* l) {
    cell* p = l;
    while (p != NULL) {
        printf("%s\t", (*p).mot);
        printf("%s\n", (*p).signification);
        p = (*p).suivant;
    }
}

size_t listlen(const cell *head) {

  size_t len = 0;
  while(head) {
    len++;
    head = head->suivant;
  }

  return len;

}



cell* trierOrdreAlphabetique(char lettre,char alphabet[26],cell *t[26]){
    cell *cell1;
    for (int i = 0; i < 26; i++) {
            if (tolower(lettre) == alphabet[i]) {
               cell1=t[i]; 
               break;
            }
    }
    
    cell *tmp, *tmp1,*temp;
    char swapMot[255];
    char swapSignification[255];
  
 
    if(cell1 == NULL)
    {
        printf("il n'ya pas de mot qui commence par la lettre %c",lettre);
        return cell1;
    }
 

    for(tmp = cell1; tmp !=NULL; tmp=tmp->suivant)
    {
        
       
        for(tmp1=tmp->suivant; tmp1 != NULL; tmp1=tmp1->suivant)
        {

            if(strcasecmp(tmp->mot,tmp1->mot)>0)
            {

                strlcpy(swapMot,tmp1->mot, 255);
                strlcpy(swapSignification,tmp1->signification, 255);
                strlcpy(tmp1->mot,tmp->mot, 255);
                strlcpy(tmp1->signification,tmp->signification, 255);
                strlcpy(tmp->mot,swapMot, 255);
                strlcpy(tmp->signification,swapSignification, 255);
               
            }
       
        }
 
    
    }

    
    return cell1;
}



cell* getList(char lettre){
    cell *cell1;
        for (int i = 0; i < 26; i++) {
            if (tolower(lettre) == alphabet[i]) {
               return t[i]; 
               break;
            }
    }
    
}


char* significationMot(char mot[255]){
        cell *cell1;
        char signification[255]={"LE MOT N'EXISTE PAS DANS LE DICTIONNAIRE"};
        char *sign=malloc(sizeof(signification));
        cell1=getList(mot[0]);
        while(cell1!=NULL){
            if(strcasecmp(cell1->mot,mot)==0){
                strlcpy(signification,cell1->signification, 255);
                
                break;
            }
            cell1=cell1->suivant;
        }
        sign=signification;
        return sign;
        
} 

void deleteMot(char mot[255],cell **cell1){

     FILE* filePointer;FILE* fp2;char str[255];char line[255];
     filePointer = fopen("test.txt", "r");
     fp2= fopen("test2.txt", "w");
        while (fgets(str, 512, filePointer)) {

        str[strcspn(str, "\r\n")] = '\0';
        strlcpy(line,str, 255);
        
         char *token = strtok(str, "=");

                    if(strcasecmp(token,mot)!=0){
                      
                        fputs(line,fp2);
                        fputs("\n", fp2);
                    }
         
                    
               
        
        }
     fclose(fp2);
     fclose(filePointer);
     remove("test.txt");
     rename("test2.txt","test.txt");
     
      
    
    struct cell* current; 
    struct cell* previous;
    
    previous=NULL;
    for(current=*cell1;current!=NULL;previous=current,current=current->suivant){
        if(strcasecmp(current->mot,mot)==0){

            if(previous==NULL){
                *cell1=current->suivant;
            }
            else{
                previous->suivant=current->suivant;
            }
            free(current);
            return;
        }
    }
    

     
}


int main(int argc, char** argv) {
    
    
     

    FILE* filePointer;

    char str[255];
    filePointer = fopen("test.txt", "r");

    while (fgets(str, 512, filePointer)) {

        str[strcspn(str, "\r\n")] = '\0';



        for (int i = 0; i < 26; i++) {
            if (tolower(str[0]) == alphabet[i]) {
                cell* p = malloc(sizeof (cell));


                char *token = strtok(str, "=");

                strlcpy(p->mot, token, 255);
                while (token != NULL) {

                    strlcpy(p->signification, token, 255);
                    token = strtok(NULL, "=");


                }
                p->suivant = t[i];
                t[i] = p;
                break;
            }

        }

    }
    print(t[11]);

        int choice;

        printf("Taper le numéro de l’opération désirée :\n");
        printf("1	Afficher la liste triée par ordre alphabétique sur les mots commençant par une lettre donnée\n");
        printf("2	Rechercher la signification d’un mot \n");
        printf("3	Supprimer un mot\n");
        printf("4	Supprimer tous les mots commençant par une lettre\n");
        printf("5	Ajouter au dictionnaire un mot donné ainsi que sa signification.\n");
        printf("6	Compter le nombre de mots commençant par une lettre donnée.\n");
        printf("7	Compter le nombre de mots du dictionnaire. \n");
        printf("8	Quitter\n");
        
        scanf("%d", &choice); 
        
        if(choice==1){
            char lettre;
            printf("choisir une lettre :\n");
            scanf(" %c",&lettre);
          
           cell* c=trierOrdreAlphabetique(lettre,alphabet,t);
            print(c);
            
        }
        if(choice==2){
            char mot[255];char signification[255];
            printf("inserer un mot :\n");
            scanf(" %s",mot);
          strlcpy(signification,significationMot(mot), 255);
                  
          printf("%s\n",signification);
           
            
        }
        if(choice==3){
            char mot[255];char signification[255];
            printf("inserer un mot :\n");
            scanf(" %s",mot);
            cell *c=getList(mot[0]);
            deleteMot(mot,&c);
            print(t[0]);

           
            
        } 
    return 0;
}



This post has been edited by blackeagle123: 03 July 2020 - 12:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Delete first ocurence from a linked list

#2 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

Re: Delete first ocurence from a linked list

Posted 03 July 2020 - 12:47 PM

i think the problem is here
cell *c=getList(mot[0]);
            deleteMot(mot,&c);


because i'm using the address of c and c is equal to t[0]
if i use t[0]=getList(mot[0]);
deleteMot(mot,&t[0])

everything work fine then

i tried something like this
deleteMot(mot,&getList(mot[0]) this doesnt work
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg   User is offline

  • member icon

Reputation: 5864
  • View blog
  • Posts: 17,837
  • Joined: 25-December 09

Re: Delete first ocurence from a linked list

Posted 03 July 2020 - 01:02 PM

Still haven't fixed your potential buffer overflow, I suggest you do that before you go any farther.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

Re: Delete first ocurence from a linked list

Posted 03 July 2020 - 01:46 PM

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * File:   main.c
 * Author: USER
 *
 * Created on June 23, 2020, 7:51 PM
 */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define BUFFER_SIZE 255

typedef struct cell {
    char mot[BUFFER_SIZE];
    char signification[BUFFER_SIZE];
    struct cell *suivant;
} cell;

char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
cell * t[26] = {0};
char mot[BUFFER_SIZE];
char signification[BUFFER_SIZE];
char lettre;

void print(cell* l) {
    cell* p = l;
    while (p != NULL) {
        printf("%s\t", (*p).mot);
        printf("%s\n", (*p).signification);
        p = (*p).suivant;
    }
}

cell* trierOrdreAlphabetique(char lettre, char alphabet[26], cell *t[26]) {

    char swapMot [BUFFER_SIZE];
    char swapSignification [BUFFER_SIZE];
    cell *cell1;
    for (int i = 0; i < 26; i++) {
        if (tolower(lettre) == alphabet[i]) {
            cell1 = t[i];
            break;
        }
    }

    cell *tmp, *tmp1, *temp;



    if (cell1 == NULL) {
        printf("il n'ya pas de mot qui commence par la lettre %c", lettre);
        return cell1;
    }


    for (tmp = cell1; tmp != NULL; tmp = tmp->suivant) {


        for (tmp1 = tmp->suivant; tmp1 != NULL; tmp1 = tmp1->suivant) {

            if (strcasecmp(tmp->mot, tmp1->mot) > 0) {

                strlcpy(swapMot, tmp1->mot, BUFFER_SIZE);
                strlcpy(swapSignification, tmp1->signification, BUFFER_SIZE);
                strlcpy(tmp1->mot, tmp->mot, BUFFER_SIZE);
                strlcpy(tmp1->signification, tmp->signification, BUFFER_SIZE);
                strlcpy(tmp->mot, swapMot, BUFFER_SIZE);
                strlcpy(tmp->signification, swapSignification, BUFFER_SIZE);

            }

        }


    }


    return cell1;
}

cell** getList(char lettre) {
    for (int i = 0; i < 26; i++) {
        if (tolower(lettre) == alphabet[i]) {
            return &t[i];
            break;
        }
    }

}

char* significationMot(char mot[BUFFER_SIZE]) {
    cell **cell1;
    char signification[BUFFER_SIZE] = {"LE MOT N'EXISTE PAS DANS LE DICTIONNAIRE"};
    char *sign = malloc(sizeof (signification));
    cell1 = getList(mot[0]);
    while (*cell1 != NULL) {
        if (strcasecmp((*cell1)->mot, mot) == 0) {
            strlcpy(signification, (*cell1)->signification, BUFFER_SIZE);

            break;
        }
        cell1 = &(*cell1)->suivant;
    }
    sign = signification;
    return sign;

}

void deleteMot(char mot[BUFFER_SIZE]) {
    cell **cell1;
    cell1 = getList(mot[0]);
    FILE* filePointer;
    FILE* fp2;
    char str[BUFFER_SIZE];
    char line[BUFFER_SIZE];
    filePointer = fopen("test.txt", "r");
    fp2 = fopen("test2.txt", "w");
    while (fgets(str, 512, filePointer)) {

        str[strcspn(str, "\r\n")] = '\0';
        strlcpy(line, str, BUFFER_SIZE);

        char *token = strtok(str, "=");

        if (strcasecmp(token, mot) != 0) {

            fputs(line, fp2);
            fputs("\n", fp2);
        }




    }
    fclose(fp2);
    fclose(filePointer);
    remove("test.txt");
    rename("test2.txt", "test.txt");



    struct cell* current;
    struct cell* previous;

    previous = NULL;
    for (current = *cell1; current != NULL; previous = current, current = current->suivant) {
        if (strcasecmp(current->mot, mot) == 0) {

            if (previous == NULL) {
                *cell1 = current->suivant;
            } else {
                previous->suivant = current->suivant;
            }
            free(current);
            //print(*cell1);
            return;
        }
    }



}

int main(int argc, char** argv) {




    FILE* filePointer;

    char str[BUFFER_SIZE];
    filePointer = fopen("test.txt", "r");

    while (fgets(str, 512, filePointer)) {

        str[strcspn(str, "\r\n")] = '\0';



        for (int i = 0; i < 26; i++) {
            if (tolower(str[0]) == alphabet[i]) {
                cell* p = malloc(sizeof (cell));


                char *token = strtok(str, "=");

                strlcpy(p->mot, token, BUFFER_SIZE);
                while (token != NULL) {

                    strlcpy(p->signification, token, BUFFER_SIZE);
                    token = strtok(NULL, "=");


                }
                p->suivant = t[i];
                t[i] = p;
                break;
            }

        }

    }
    print(t[11]);

    int choice;

    printf("Taper le numéro de l’opération désirée :\n");
    printf("1	Afficher la liste triée par ordre alphabétique sur les mots commençant par une lettre donnée\n");
    printf("2	Rechercher la signification d’un mot \n");
    printf("3	Supprimer un mot\n");
    printf("4	Supprimer tous les mots commençant par une lettre\n");
    printf("5	Ajouter au dictionnaire un mot donné ainsi que sa signification.\n");
    printf("6	Compter le nombre de mots commençant par une lettre donnée.\n");
    printf("7	Compter le nombre de mots du dictionnaire. \n");
    printf("8	Quitter\n");

    scanf("%d", &choice);

    if (choice == 1) {
        char lettre;
        printf("choisir une lettre :\n");
        scanf(" %c", &lettre);

        cell* c = trierOrdreAlphabetique(lettre, alphabet, t);
        print(c);

    }
    if (choice == 2) {

        printf("inserer un mot :\n");
        scanf(" %s", mot);
        strlcpy(signification, significationMot(mot), BUFFER_SIZE);

        printf("%s\n", signification);


    }
    if (choice == 3) {

        printf("inserer un mot :\n");
        scanf(" %s", mot);

        deleteMot(mot);
        print(t[0]);



    }
    return 0;
}




now everything works fine had to make getList return &t[i] instead of t[i];
and i think i fixed the overflow issue
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5864
  • View blog
  • Posts: 17,837
  • Joined: 25-December 09

Re: Delete first ocurence from a linked list

Posted 03 July 2020 - 01:52 PM

You still have a potential buffer overflow you need to fix!

What's with all of those horrible global variables?

Look what my compiler says about your current code:

||=== Build: Debug in chomework (compiler: gcc9-2) ===|
main.c|33|warning: no previous declaration for ‘print’ [-Wmissing-declarations]|
main.c|42|warning: no previous declaration for ‘trierOrdreAlphabetique’ [-Wmissing-declarations]|
main.c||In function ‘trierOrdreAlphabetique’:|
main.c|42|warning: declaration of ‘lettre’ shadows a global declaration [-Wshadow]|
main.c|31|note: shadowed declaration is here|
main.c|42|warning: declaration of ‘alphabet’ shadows a global declaration [-Wshadow]|
main.c|27|note: shadowed declaration is here|
main.c|42|warning: declaration of ‘t’ shadows a global declaration [-Wshadow]|
main.c|28|note: shadowed declaration is here|
main.c|69|error: implicit declaration of function ‘strcasecmp’; did you mean ‘strncmp’? [-Wimplicit-function-declaration]|
main.c|71|error: implicit declaration of function ‘strlcpy’; did you mean ‘strncpy’? [-Wimplicit-function-declaration]|
main.c|54|warning: unused variable ‘temp’ [-Wunused-variable]|
main.c|89|warning: no previous declaration for ‘getList’ [-Wmissing-declarations]|
main.c||In function ‘getList’:|
main.c|89|warning: declaration of ‘lettre’ shadows a global declaration [-Wshadow]|
main.c|31|note: shadowed declaration is here|
main.c|99|warning: no previous declaration for ‘significationMot’ [-Wmissing-declarations]|
main.c||In function ‘significationMot’:|
main.c|99|warning: declaration of ‘mot’ shadows a global declaration [-Wshadow]|
main.c|29|note: shadowed declaration is here|
main.c|101|warning: declaration of ‘signification’ shadows a global declaration [-Wshadow]|
main.c|30|note: shadowed declaration is here|
main.c|117|warning: no previous declaration for ‘deleteMot’ [-Wmissing-declarations]|
main.c||In function ‘deleteMot’:|
main.c|117|warning: declaration of ‘mot’ shadows a global declaration [-Wshadow]|
main.c|29|note: shadowed declaration is here|
main.c||In function ‘main’:|
main.c|228|warning: declaration of ‘lettre’ shadows a global declaration [-Wshadow]|
main.c|31|note: shadowed declaration is here|
main.c|172|warning: unused parameter ‘argc’ [-Wunused-parameter]|
main.c|172|warning: unused parameter ‘argv’ [-Wunused-parameter]|
main.c||In function ‘getList’:|
main.c|97|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 2 error(s), 17 warning(s) (0 minute(s), 0 second(s)) ===|



Doesn't seem too fixed to me.


Jim
Was This Post Helpful? 2
  • +
  • -

#6 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

Re: Delete first ocurence from a linked list

Posted 03 July 2020 - 01:57 PM

What ide are you using for some reasons it runs without any errors for me
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is offline

  • member icon

Reputation: 5864
  • View blog
  • Posts: 17,837
  • Joined: 25-December 09

Re: Delete first ocurence from a linked list

Posted 03 July 2020 - 02:02 PM

That's probably because you're using an improperly configured compiler, and using non-standard C functions.

The question is what compiler/IDE are you using, and how do you have your compiler/IDE configured?

By default most IDEs only report the most serious of "errors" you need to manually configure the IDE to help you by reporting most of the common error/warnings.


EDIT: And as I said in my last post you still have a buffer overflow exploit waiting to happen.

EDIT2: Here is my build configuration:

gcc -Wall -Wshadow -Winit-self -Wredundant-decls -Wcast-align -Wfloat-equal -Winline -Wunreachable-code -Wmissing-declarations -Wmissing-include-dirs -Wswitch-enum -Wswitch-default -Wmain -pedantic-errors -pedantic -Wextra -Wall -g -Wvla -std=c11



Jim

This post has been edited by jimblumberg: 03 July 2020 - 02:05 PM

Was This Post Helpful? 2
  • +
  • -

#8 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

Re: Delete first ocurence from a linked list

Posted 05 July 2020 - 12:32 AM

Ok then lets go step by step first thing the buffer overflow though it was fixed now i'm confused on how to fix it
Was This Post Helpful? 0
  • +
  • -

#9 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

Re: Delete first ocurence from a linked list

Posted 05 July 2020 - 01:30 AM

cd 'C:\Users\USER\Documents\NetBeansProjects\ProjetC'
cmd.exe /c rm -rf build/Debug/Cygwin-Windows/main.o

CLEAN SUCCESSFUL (total time: 154ms)
cd 'C:\Users\USER\Documents\NetBeansProjects\ProjetC'
C:\cygwin64\bin\make.exe -f nbproject/Makefile-Debug.mk build/Debug/Cygwin-Windows/main.o
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/main.o.d"
gcc    -c -g -MMD -MP -MF "build/Debug/Cygwin-Windows/main.o.d" -o build/Debug/Cygwin-Windows/main.o main.c

COMPILE FILE SUCCESSFUL (total time: 1s)

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7456
  • View blog
  • Posts: 25,098
  • Joined: 05-May 12

Re: Delete first ocurence from a linked list

Posted 05 July 2020 - 01:58 AM

Add "-Wall" to your gcc compile options in Netbeans. I don't use Netbeans, so I don't know how to do that. You'll need to look it up yourself.

Your buffer overflow waiting to happen is still on line 126.
Was This Post Helpful? 1
  • +
  • -

#11 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

Re: Delete first ocurence from a linked list

Posted 05 July 2020 - 10:20 AM

Working on fixing some of the following errors but on strcmp how am i supposed to make it case insensitive after searching on google lots of people say to use strcasecmp instead for case insensitive

This post has been edited by blackeagle123: 05 July 2020 - 10:41 AM

Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg   User is offline

  • member icon

Reputation: 5864
  • View blog
  • Posts: 17,837
  • Joined: 25-December 09

Re: Delete first ocurence from a linked list

Posted 05 July 2020 - 11:18 AM

Well you can use strcasecmp(), but you need to realize that this is a non-C standard function.

If you want non-case sensitivity in standard C you will need to create your own comparison function that is case insensitive, this function should return the same values as strcmp() (< 0, 0, > 0).

Did you fix the buffer overflow problem?

Did you get rid of all of those horrible global variables?


Jim

This post has been edited by jimblumberg: 05 July 2020 - 11:20 AM

Was This Post Helpful? 1
  • +
  • -

#13 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

Re: Delete first ocurence from a linked list

Posted 05 July 2020 - 12:27 PM

View PostSkydiver, on 05 July 2020 - 01:58 AM, said:

Add "-Wall" to your gcc compile options in Netbeans. I don't use Netbeans, so I don't know how to do that. You'll need to look it up yourself.

Your buffer overflow waiting to happen is still on line 126.


fgets(str, 512, filePointer)) this is line 126 i dont see how this will cause buffer overflow
wait this must be char str[BUFFER_SIZE];
512 instead of buffer_size ?

This post has been edited by blackeagle123: 05 July 2020 - 12:29 PM

Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg   User is offline

  • member icon

Reputation: 5864
  • View blog
  • Posts: 17,837
  • Joined: 25-December 09

Re: Delete first ocurence from a linked list

Posted 05 July 2020 - 12:36 PM

Quote

wait this must be char str[BUFFER_SIZE];
512 instead of buffer_size ?


Yes! The number inside the fgets() call must be equal to or less than the size of your array.

Jim
Was This Post Helpful? 1
  • +
  • -

#15 blackeagle123   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 131
  • Joined: 28-November 13

Re: Delete first ocurence from a linked list

Posted 05 July 2020 - 12:39 PM

I fixed some problems can you please take a look at my code now and tell me if i still have problems

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * File:   main.c
 * Author: USER
 *
 * Created on June 23, 2020, 7:51 PM
 */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define BUFFER_SIZE 255


typedef struct cell {
    char mot[BUFFER_SIZE];
    char signification[BUFFER_SIZE];
    struct cell *suivant;
} cell;

char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char mot[BUFFER_SIZE];
cell * t[26] = {0};

void print(cell* l);
cell* trierOrdreAlphabetique(char lettre);
char* significationMot();
cell** getList(char lettre);
void deleteMot();


void print(cell* l) {
    cell* p = l;
    while (p != NULL) {
        printf("%s\t", (*p).mot);
        printf("%s\n", (*p).signification);
        p = (*p).suivant;
    }
}

cell* trierOrdreAlphabetique(char lettre) {
char swapMot[BUFFER_SIZE];
char swapSignification[BUFFER_SIZE];

    cell *cell1;
    for (int i = 0; i < 26; i++) {
        if (tolower(lettre) == alphabet[i]) {
            cell1 = t[i];
            break;
        }
    }

    cell *tmp, *tmp1;

    if (cell1 == NULL) {
        printf("il n'ya pas de mot qui commence par la lettre %c", lettre);
        return cell1;
    }

    for (tmp = cell1; tmp != NULL; tmp = tmp->suivant) {
        for (tmp1 = tmp->suivant; tmp1 != NULL; tmp1 = tmp1->suivant) {
            if (strcasecmp(tmp->mot, tmp1->mot) > 0) {
                strlcpy(swapMot, tmp1->mot, BUFFER_SIZE);
                strlcpy(swapSignification, tmp1->signification, BUFFER_SIZE);
                strlcpy(tmp1->mot, tmp->mot, BUFFER_SIZE);
                strlcpy(tmp1->signification, tmp->signification, BUFFER_SIZE);
                strlcpy(tmp->mot, swapMot, BUFFER_SIZE);
                strlcpy(tmp->signification, swapSignification, BUFFER_SIZE);
            }
        }
    }
    return cell1;
}

cell** getList(char lettre) {
    for (int i = 0; i < 26; i++) {
        if (tolower(lettre) == alphabet[i]) {
            return &t[i];
            break;
        }
    }
    return NULL;
}

char* significationMot() {
    cell **cell1;
     char signification[] = {"LE MOT N'EXISTE PAS DANS LE DICTIONNAIRE"};
    char *sign = malloc(sizeof (signification));
    cell1 = getList(mot[0]);
    while (*cell1 != NULL) {
        if (strcasecmp((*cell1)->mot, mot) == 0) {
            strlcpy(signification, (*cell1)->signification, BUFFER_SIZE);

            break;
        }
        cell1 = &(*cell1)->suivant;
    }
    sign = signification;
    return sign;
}

void deleteMot() {
    cell **cell1;
    cell1 = getList(mot[0]);
    FILE* filePointer;
    FILE* fp2;
    char str[512];
    char line[BUFFER_SIZE];
    
    filePointer = fopen("test.txt", "r");
    fp2 = fopen("test2.txt", "w");
    while (fgets(str, 512, filePointer)) {
        str[strcspn(str, "\r\n")] = '\0';
        strlcpy(line, str, BUFFER_SIZE);
        char *token = strtok(str, "=");
        if (strcasecmp(token, mot) != 0) {
            fputs(line, fp2);
            fputs("\n", fp2);
        }
    }
    fclose(fp2);
    fclose(filePointer);
    remove("test.txt");
    rename("test2.txt", "test.txt");
    
    struct cell* current;
    struct cell* previous;
    previous = NULL;
    for (current = *cell1; current != NULL; previous = current, current = current->suivant) {
        if (strcasecmp(current->mot, mot) == 0) {
            if (previous == NULL) {
                *cell1 = current->suivant;
            } else {
                previous->suivant = current->suivant;
            }
            free(current);
            return;
        }
    }
}

int main() {
char signification[BUFFER_SIZE];
    FILE* filePointer;
    char str[BUFFER_SIZE];
    filePointer = fopen("test.txt", "r");
    while (fgets(str, 512, filePointer)) {
        str[strcspn(str, "\r\n")] = '\0';
        for (int i = 0; i < 26; i++) {
            if (tolower(str[0]) == alphabet[i]) {
                cell* p = malloc(sizeof (cell));
                char *token = strtok(str, "=");
                strlcpy(p->mot, token, BUFFER_SIZE);
                while (token != NULL) {
                    strlcpy(p->signification, token, BUFFER_SIZE);
                    token = strtok(NULL, "=");
                }
                p->suivant = t[i];
                t[i] = p;
                break;
            }
        }
    }

    int choice;

    printf("Taper le numéro de l’opération désirée :\n");
    printf("1	Afficher la liste triée par ordre alphabétique sur les mots commençant par une lettre donnée\n");
    printf("2	Rechercher la signification d’un mot \n");
    printf("3	Supprimer un mot\n");
    printf("4	Supprimer tous les mots commençant par une lettre\n");
    printf("5	Ajouter au dictionnaire un mot donné ainsi que sa signification.\n");
    printf("6	Compter le nombre de mots commençant par une lettre donnée.\n");
    printf("7	Compter le nombre de mots du dictionnaire. \n");
    printf("8	Quitter\n");

    scanf("%d", &choice);

    if (choice == 1) {
        char lettre;
        printf("choisir une lettre :\n");
        scanf(" %c", &lettre);
        cell* c = trierOrdreAlphabetique(lettre);
        print(c);
    }
    
    if (choice == 2) {
        printf("inserer un mot :\n");
        scanf(" %s", mot);
        strlcpy(signification, significationMot(mot), BUFFER_SIZE);
        printf("%s\n", signification);
    }
    
    if (choice == 3) {
        printf("inserer un mot :\n");
        scanf(" %s", mot);
        deleteMot(mot);
    }
    
    return 0;
}



Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2