IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

erreur de segmentation


Sujet :

C

  1. #1
    Nouveau Candidat au Club
    Inscrit en
    Décembre 2012
    Messages
    5
    Détails du profil
    Informations forums :
    Inscription : Décembre 2012
    Messages : 5
    Points : 1
    Points
    1
    Par défaut
    bonsoir, j'ai un problème avec mon code au niveau de la concaténation de deux chaines de caractères avec la fonction strcat, voilà mon code
    merci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    # include <stdio.h>
    # include <math.h>
    #include <stdlib.h>
    #include <unistd.h>
    # include <string.h>
    int main(int argc,char *argv[])
      {
        int i=0;
        char *text;
        int taille,t ;
        int j=0;
        int tab1[strlen(text)];
        int tab2[strlen(argv[1])];
        char s[7];
        int taille_m;
        char car[2]={' ',' '};
        char mot[2];
        char table[26];
        table[0]='A' ;
        table[1]='B' ;
        table[2]='C' ;
        table[3]='D' ;
        table[4]='E' ;
        table[5]='F' ;
        table[6]='G' ;
        table[7]='H' ;
        table[8]='I' ;
        table[9]='J' ;
        table[10]='K' ;
        table[11]='L' ;
        table[12]='M' ;
        table[13]='N' ;
        table[14]='O' ;
        table[15]='P' ;
        table[16]='Q' ;
        table[17]='R' ;
        table[18]='S' ;
        table[19]='T' ;
        table[20]='U' ;
        table[21]='V' ;
        table[22]='W' ;
        table[23]='X' ;
        table[24]='Y' ;
        table[25]='Z' ;
        printf("entrez le texte\n");
        scanf("%s",text);
        printf("%s",text);
        //s="bnsp";
        taille=strlen(text);
        printf("%d\n",taille);
        // strcpy(car," \0");
        //car[0]=" ";
        car[1]='\0';
        strcpy(mot,"aa");
        strcpy(s," ");
    	printf( "bonjour");
    // I retrieves the index of each character in the text that corresponds to the letter of the table
        for (i=0;i<=taille;i++)
         {
          while ((j<taille)&&(text[i]!=table[j]))
           {
    	j++;
           }
    	if (text[i]==table[j]) 
             {
    	   tab1[i]=j;
    	 }
    	else
    	 if (j>taille)
    	 {
    	   tab1[i]=100;
    	 }
         }
    // I retrieves the index of each character of the key that corresponds to the letter of the table
          t=strlen(argv[1]);
          printf("%d\n",t);
          j=0;
          for (i=0;i<=t;i++)
    	{
          while ((j<t)&&(text[i]!=argv[1][i]))
           {
    	j++;
           }
    	if (argv[1][i]!=table[j]) 
             {
    	   tab2[i]=j;
    	 }
     
    	}
    //
     
     for (i=0;i<=taille;i++)
         {	 
    	if (tab1[i]!=100)
    	   {
    	    if (i<t)
    		{
    		 if ((tab1[i]+tab2[i])<26)
    		   {
     
    		    strcpy(s,mot);
           		    car[0]=table[tab1[i]+tab2[i]];
    		    printf("%c\n",car[0]);
    		    strcat(s,car);
     		    printf("%s\n",s);
     
    		   }
    		 else
    		   {
    		    strcpy(s,mot);
                        car[0]=table[tab1[i]+tab2[i]-26];
     
    		    strcat(s,car);
     		    printf("%s\n",s);		    
     
    		   }
    		}
    	     else
    		{
    		  if ((tab1[i]+tab2[i-t])<26)
    		   {
    		    strcpy(s,mot);
    		    car[0]=table[tab1[i]+tab2[i]];
     
    		    strcat(s,s);
     		    printf("%s\n",car);
     
    		   }
    		  else
    		   {
                        strcpy(s,mot);
    		    car[0]=table[tab1[i]+tab2[i-t]-26];
     
    		    strcat(s,car);
     		    printf("%s\n",s);
     
    		   }
    		}
    	   }
    	else
    	  {
    	   strcpy(s,mot);
    	   car[0]=text[i];
     
    	   strcat(s,car);
     
    	  }
     
         }
    printf("ciphertext\n");
    printf("%s\n",s);		 
    return 0;
    }

  2. #2
    Membre expérimenté Avatar de Trademark
    Profil pro
    Inscrit en
    Février 2009
    Messages
    762
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2009
    Messages : 762
    Points : 1 396
    Points
    1 396
    Par défaut
    Salut,

    Ton code est une pure misère à lire, il faut absolument que tu apprennes à découper en fonction.

    Je passerai les détails du langage pour en venir directement au fait mais à mon avis tu ferais bien de prendre un bon livre en C.

    Dans ta boucle for :

    Or text à une taille comprise entre 0 et taille-1. Tu dois donc faire i < taille.

    Bon courage pour la suite.

  3. #3
    Expert éminent sénior
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Points : 13 926
    Points
    13 926
    Par défaut
    Quelques remarques :
    -
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    int main(int argc,char *argv[])
      {
        int i=0;
        char *text;
     ...
        printf("entrez le texte\n");
        scanf("%s",text);
    Il n'y a pas de place pour stocker la chaine de caractères lue : Il faut un tableau de char et non pas un simple pointeur (text). Un pointeur est fait pour stocker une adresse, pas une chaine.

    - Pourquoi ne pas initialiser le tableau table comme tu l'as fait avec car
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    char table[26] = { 'A','B',.......'Z'};
    //ou encore
    char table [27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    -
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    char mot[2];
    ...
    strcpy(mot,"aa");
    le tableau mot est trop petit car strcpy() va copier également le caractère '\0' qui marque la fin de la chaine "aa". Il doit comporter au minimum 3 char.
    Publication : Concepts en C

    Mon avatar : Glenn Gould

    --------------------------------------------------------------------------
    Une réponse vous a été utile ? Remerciez son auteur en cliquant le pouce vert !

Discussions similaires

  1. Erreurs de segmentation !
    Par anti-conformiste dans le forum Applications et environnements graphiques
    Réponses: 16
    Dernier message: 18/10/2005, 11h11
  2. Erreur de segmentation
    Par Trunks dans le forum C
    Réponses: 3
    Dernier message: 06/10/2005, 18h28
  3. Erreur de segmentation (Inconnue)
    Par Dark-Meteor dans le forum C
    Réponses: 5
    Dernier message: 08/09/2005, 13h42
  4. [Dev-C++] Erreur de segmentation...
    Par sas dans le forum Dev-C++
    Réponses: 11
    Dernier message: 26/03/2005, 14h25
  5. erreur de segmentation
    Par transistor49 dans le forum C++
    Réponses: 10
    Dernier message: 15/03/2005, 11h18

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo