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 :

Visual C++ Express. Mettre une chaine de caractères dans un tableau de caractères


Sujet :

C++

  1. #1
    Membre du Club Avatar de Gilles57-H-G
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    88
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 88
    Points : 62
    Points
    62
    Par défaut Visual C++ Express. Mettre une chaine de caractères dans un tableau de caractères
    Bonjour.

    Je cherche à faire un manipulation simple.

    Je veux créer un chaine de caractère, et un tableau de caractère.

    Mettre chaque caractère dans un élément du tableau.

    En basic il suffit d'utiliser la fonction split.

    Ex

    table[]
    chaine = "abcdef"

    table Split(chaine).

    je veux avoir
    a dans le premier élément du tableau table, b dans le deuxième élément du tableau table et ainsi de suite.

    J'ai volontairement omis les paramètres de split.

    Je cherche un manière simple, sans pointeur.

  2. #2
    Membre chevronné Avatar de Astraya
    Homme Profil pro
    Consommateur de café
    Inscrit en
    Mai 2007
    Messages
    1 043
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France

    Informations professionnelles :
    Activité : Consommateur de café
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2007
    Messages : 1 043
    Points : 2 234
    Points
    2 234
    Par défaut
    simple question! Pourquoi sans pointeurs?

    Sans pointeur ça sera dur.
    Homer J. Simpson


  3. #3
    Membre du Club Avatar de Gilles57-H-G
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    88
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 88
    Points : 62
    Points
    62
    Par défaut
    C'est pas que je ne veux pas de pointeur, simplement en Vb, c'est super simple.

    Je voudrais la même en C++.

    j'ai pas fait de C++ depuis pas mal de temps

    Je veux refaire ça en C++.

    Code VB :

    Code vb : 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
    Dim fo As New Form2
     
            Dim t1() As String
            Dim t2() As String
            Dim t3() As String
            Dim t4() As String
            Dim t5() As String
            Dim t6() As String
            Dim t7() As String
            Dim t8() As String
            Dim t9() As String
            Dim t10() As String
     
     
            Dim cont As Long
            Dim test As Integer
            test = 0
     
            Dim a, b, c, d, ee, f, g, h, i, j As Integer
     
            Dim mot As String
            Dim alpha As String
            alpha = "a,b,c,d"
     
            t1 = Split(alpha, ",")
            t2 = Split(alpha, ",")
            t3 = Split(alpha, ",")
            t4 = Split(alpha, ",")
            t5 = Split(alpha, ",")
            t6 = Split(alpha, ",")
            t7 = Split(alpha, ",")
            t8 = Split(alpha, ",")
            t9 = Split(alpha, ",")
            t10 = Split(alpha, ",")
     
            Dim top As Integer
            top = UBound(t10)
     
     
            While j < top + 1
     
     
                For a = 0 To top
                    cont = cont + 1
                    mot = t1(j) & t2(i) & t3(h) & t4(g) & t5(f) & t6(ee) & t7(d) & t8(c) & t9(b) & t10(a)
     
                    Select Case test
                        Case 0
                            fo.ListBox1.Items.Add(mot)
                        Case 1
                            fo.ListBox2.Items.Add(mot)
                    End Select
     
     
                    If a = top And b = top And c = top And d = top And ee = top And f = top And g = top And h = top And i = top Then
                        b = 0
                        c = 0
                        d = 0
                        ee = 0
                        f = 0
                        g = 0
                        h = 0
                        i = 0
                        j = j + 1
                        Exit For
                    End If
     
                    If a = top And b = top And c = top And d = top And ee = top And f = top And g = top And h = top Then
                        b = 0
                        c = 0
                        d = 0
                        ee = 0
                        f = 0
                        g = 0
                        h = 0
                        i = i + 1
     
                        Exit For
                    End If
     
     
     
                    If a = top And b = top And c = top And d = top And ee = top And f = top And g = top Then
                        b = 0
                        c = 0
                        d = 0
                        ee = 0
                        f = 0
                        g = 0
                        h = h + 1
                        Exit For
                    End If
     
     
        If a = top And b = top And c = top And d = top And ee = top And f = top Then
                        test = 1
                        b = 0
                        c = 0
                        d = 0
                        ee = 0
                        f = 0
                        g = g + 1
                        Exit For
                    End If
     
               If a = top And b = top And c = top And d = top And ee = top Then
                        b = 0
                        c = 0
                        d = 0
                        ee = 0
                        f = f + 1
                        Exit For
                    End If
     
     
                    If a = top And b = top And c = top And d = top Then
                        b = 0
                        c = 0
                        d = 0
                        ee = ee + 1
                        Exit For
                    End If
     
     
                    If a = top And b = top And c = top Then
                        b = 0
                        c = 0
                        d = d + 1
                        Exit For
                    End If
     
     
                    If a = top And b = top Then
                        b = 0
                        c = c + 1
                        Exit For
                    End If
     
                    '
                    If a = top Then
                        b = b + 1
                        Exit For
                    End If
     
     
                Next
     
            End While
            fo.Text = cont
            fo.Show()

  4. #4
    Membre chevronné Avatar de Astraya
    Homme Profil pro
    Consommateur de café
    Inscrit en
    Mai 2007
    Messages
    1 043
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France

    Informations professionnelles :
    Activité : Consommateur de café
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2007
    Messages : 1 043
    Points : 2 234
    Points
    2 234
    Par défaut
    Mais est tu au courant que en VB, tu manipules des objets comme si tu manipulais des références en C++? En C++, référence et pointeur est équivalent sur certains aspects.

    Sinon tu peux trouver la longueur de ta chaine, créer un tableau ( un std::vector de char ).Dans un boucle for, copie les caractères un a un dans un nouveau noeud du vector avec un push_back.

    Avec des iterateurs ( abstraction de séquence : pointeur ), tu y arriveras de façon rapide, simple et très efficace. Plus que sans pointeur.
    Homer J. Simpson


  5. #5
    Membre du Club Avatar de Gilles57-H-G
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    88
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 88
    Points : 62
    Points
    62
    Par défaut
    Merci.

    J'ai trouvé ceci, je vais l'exploiter :

    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
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
     
    // Retour tableau des chaines recupérer. Terminé par NULL. 
    // chaine : chaine à splitter 
    // delim : delimiteur qui sert à la decoupe 
    // vide : 0 : on n'accepte pas les chaines vides 
    // 1 : on accepte les chaines vides 
    char** split(char* chaine,const char* delim,int vide){ 
    char** tab=NULL; //tableau de chaine, tableau resultat 
    char *ptr; //pointeur sur une partie de 
    int sizeStr; //taille de la chaine à recupérer 
    int sizeTab=0; //taille du tableau de chaine 
    char* largestring; //chaine à traiter 
    int sizeDelim=strlen(delim); //taille du delimiteur 
    largestring = chaine; //comme ca on ne modifie pas le pointeur d'origine 
    //(faut ke je verifie si c bien nécessaire) 
    while( (ptr=strstr(largestring, delim))!=NULL ){ 
    sizeStr=ptr-largestring; 
    //si la chaine trouvé n'est pas vide ou si on accepte les chaine vide 
    if(vide==1 || sizeStr!=0){ 
    //on alloue une case en plus au tableau de chaines 
    sizeTab++; 
    tab= (char**) realloc(tab,sizeof(char*)*sizeTab); 
    //on alloue la chaine du tableau 
    tab[sizeTab-1]=(char*) malloc( sizeof(char)*(sizeStr+1) ); 
    strncpy(tab[sizeTab-1],largestring,sizeStr); 
    tab[sizeTab-1][sizeStr]='\0'; 
    } 
    //on decale le pointeur largestring pour continuer la boucle apres le premier elément traiter 
    ptr=ptr+sizeDelim; 
    largestring=ptr; 
    } 
    //si la chaine n'est pas vide, on recupere le dernier "morceau" 
    if(strlen(largestring)!=0){ 
    sizeStr=strlen(largestring); 
    sizeTab++; 
    tab= (char**) realloc(tab,sizeof(char*)*sizeTab); 
    tab[sizeTab-1]=(char*) malloc( sizeof(char)*(sizeStr+1) ); 
    strncpy(tab[sizeTab-1],largestring,sizeStr); 
    tab[sizeTab-1][sizeStr]='\0'; 
    } 
    else if(vide==1){ //si on fini sur un delimiteur et si on accepte les mots vides,on ajoute un mot vide 
    sizeTab++; 
    tab= (char**) realloc(tab,sizeof(char*)*sizeTab); 
    tab[sizeTab-1]=(char*) malloc( sizeof(char)*1 ); 
    tab[sizeTab-1][0]='\0'; 
    } 
    //on ajoute une case à null pour finir le tableau 
    sizeTab++; 
    tab= (char**) realloc(tab,sizeof(char*)*sizeTab); 
    tab[sizeTab-1]=NULL; 
    return tab; 
    } 
     
     
     
     
     
     
    int main(){ 
    int i; 
    int ret; 
    char* str="foo|bar||baz|bar|"; 
    char** tab; 
    printf("Chaine initiale : %s \n",str); 
    tab=split(str,"|",0); 
    //affichage du resultat 
    for(i=0;tab[i]!=NULL;i++) { 
    printf("%d : %s\n",i,tab[i]); 
    //au passge je désalloue les chaines 
    free(tab[i]); 
    } 
    free(tab); 
    printf("\n"); 
    tab=split(str,"|",1); 
    //affichage du resultat 
    for(i=0;tab[i]!=NULL;i++) { 
    printf("%d : %s\n",i,tab[i]); 
    //au passge je désalloue les chaines 
    free(tab[i]); 
    } 
    free(tab); 
    system("pause"); 
    }

  6. #6
    Membre chevronné Avatar de Astraya
    Homme Profil pro
    Consommateur de café
    Inscrit en
    Mai 2007
    Messages
    1 043
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France

    Informations professionnelles :
    Activité : Consommateur de café
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2007
    Messages : 1 043
    Points : 2 234
    Points
    2 234
    Par défaut
    J'ai trouvé ceci, je vais l'exploiter :
    ça utilise un pointeur
    Homer J. Simpson


  7. #7
    Membre du Club Avatar de Gilles57-H-G
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    88
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 88
    Points : 62
    Points
    62
    Par défaut
    Certes, je voulais un truc plus simple, je pensais que c'était possible sans pointeurs.

    Le Split en VB et quand même plus simple.

  8. #8
    Rédacteur/Modérateur
    Avatar de JolyLoic
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2004
    Messages
    5 463
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2004
    Messages : 5 463
    Points : 16 213
    Points
    16 213
    Par défaut
    Ce code est plus du C que du C++, et on peut dire que même s'il compile en C++, ce n'est pas du C++ propre.

    En C++, pour splitter une chaîne, c'est assez simple (et sans pointeur) :

    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
    #include <regex>
    #include <string>
    #include <iostream>
     
    using namespace std;
     
    int main()
    {
    	string maChaine = "Une chaine, a decouper, selon les espaces (ou les ponctuations)";
    	regex r(" "); // Je conseille de refaire le test avec "[, ()]+", le résultat sera meilleur
    	const sregex_token_iterator end;
    	int i=0;
    	for (sregex_token_iterator it(maChaine.begin(), maChaine.end(), r, -1); it != end; ++it) 
    	{
    		++i;
    	    cout << "Chaine " << i << " : " << *it << endl;
    	}
    }
    (remarque : Avec un compilateur trop vieux, il faudra remplace std::regex par std::tr1::regex, voire installer la biblitohèque boost pour avoir accès à boost::regex).
    Ma session aux Microsoft TechDays 2013 : Développer en natif avec C++11.
    Celle des Microsoft TechDays 2014 : Bonnes pratiques pour apprivoiser le C++11 avec Visual C++
    Et celle des Microsoft TechDays 2015 : Visual C++ 2015 : voyage à la découverte d'un nouveau monde
    Je donne des formations au C++ en entreprise, n'hésitez pas à me contacter.

  9. #9
    Membre du Club Avatar de Gilles57-H-G
    Profil pro
    Inscrit en
    Novembre 2010
    Messages
    88
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2010
    Messages : 88
    Points : 62
    Points
    62
    Par défaut
    Merci.

    J'ai construit ceci, il n'y a pas plus basic, sans vilain jeu de mot :


    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
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include   <cstring> 
    #include <iostream>
    #include   <cstdlib> 
    #include <string> 
    using namespace std;
     
    int main(){ 
    int i; 
    int taille;
    int ret; 
    string chaine("abcdefgh");
     
    taille = chaine.length();
    string tableau[taille];
     
     
    for (i=0; i<taille; i++)
     
    { tableau[i] = chaine.substr(i,1);
     
    cout<< tableau[i]<<endl;
                                     }
     
    system("pause"); 
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Mettre une chaine de caractères dans un tableau de caractères
    Par Gilles57-H-G dans le forum Visual C++
    Réponses: 3
    Dernier message: 03/12/2010, 10h49
  2. Réponses: 6
    Dernier message: 14/02/2008, 16h32
  3. Réponses: 31
    Dernier message: 02/11/2007, 16h18
  4. [vb.net 1.1] Mettre une image de fond dans un tableau
    Par malhivertman1 dans le forum Windows Forms
    Réponses: 6
    Dernier message: 21/11/2006, 16h32
  5. [Debutant] Mettre une chaine de caractere dans un tableau
    Par bouboussjunior dans le forum Collection et Stream
    Réponses: 4
    Dernier message: 14/09/2004, 11h33

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