Bonjour,
Je veux découper une chaine de caractère et la stocker dans un tableau de chaine.
voici ma chaine: ac[cg]tg[zu]op
Je veux découper cette chaine et stocker chaque caractère dans une case d'un tableau de chaine mais la chaine entre crocher doit être dans une seule case du tableau.
par exemple mon tableau de chaine sera de cette façon:
tableau[0]=a
tableau[1]=c
tableau[2]=cg
tableau[3]=t
tableau[4]=g
tableau[5]=zu
tableau[6]=o
tableau[7]=p.
voici mon code mais je pense que ça marche pas.
est-ce que vous pouvez m'aider?
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 #include <stdio.h> #include <stdlib.h> #include <string.h> void add_caractere(char* s,char c) { int t=strlen(s); realloc(s,t+1); s[t]=c; s[t+1]='\0'; } int main() { char* motif; motif=malloc(sizeof(char*)*200); char* motif1; motif=malloc(sizeof(char*)*20); strcpy(motif,"ac[cg]tg[zu]opt"); char* tableau[strlen(motif)]; int i=0; int j=0; int t=0; printf("\n le motif est: %s",motif); for(i=0;i<strlen(motif);i++) {tableau[i]=NULL;} i=0; while(i<strlen(motif)) { if(motif[i]=='[') { t=i; while (motif[t]!=']') { motif1= motif1 + motif[i]; t++; } } else { add_caractere(tableau[j],motif[i]); //tableau[j]=motif[i]; j++; } i++; } for(i=0;i<j+1;i++) // pour voir le resultat printf("tab[%d]: %s",i,tableau[i]); return 0; }
Merci
Partager