Concatenation : contenu d'un element dela lis
kazouu Messages postés 466 Statut Membre -
jai une fonction qui boucle sur tte une liste chaine en affichant le contenu , le probleme c'est que je sais pas comment faire pour concatener le contenu et une chaine de caractere (espace) lors de l'aafichage
merci
je compte sur vous
Configuration: Windows XP Internet Explorer 7.0
21 réponses
- 1
- 2
Le problème porte sur la concaténation des éléments d'une liste de chaînes en C avec un espace lors de l'affichage, afin d'obtenir une phrase constituée des mots successifs sans perte. La solution clé consiste à déclarer et allouer une chaîne tampon, puis, dans la boucle, utiliser strcat(concat, p->mot) et afficher le mot, en veillant à inclure <string.h>. D'autres détails pratiques incluent la gestion correcte de la mémoire pour éviter les débordements et l'assurance que concat est bien dimensionné avant d'appeler strcat à chaque itération. En pratique, il faut dimensionner le buffer en fonction du nombre et de la longueur des mots et prévoir une étape optionnelle pour gérer les espaces finaux.
-
-
man strcat ?
http://www.linux-kheops.com/doc/man/manfr/man-html-0.9/man3/strcat.3.html -
-
-
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question -
tu déclares un char* concat
par exemple et a chaque fois dans ta fonction tu fais
void AfficherListe(Liste *L){
Liste *p;
for(p=L;p!=NULL;p=p->suivant){
strcat(concat,p->mot);
AfficheMot(p->mot);
AfficherCoordonnees(p->c);
}
}
en faisant gaffe de bien alloué ta chaine concat et #include <string.h> -
la fonction concatener est une fonction qui regroupe deux chaines de caractéres
tu veux regrouper quelles chaines? -
tu n'as pas compris ta chaine concat est la chaine ou il y aura tout tes mot si tu veux rajouter un espace tu fais
void AfficherListe(Liste *L){
Liste *p;
for(p=L;p!=NULL;p=p->suivant){
strcat(concat,p->mot);
strcat(concat,' ');
AfficheMot(p->mot);
AfficherCoordonnees(p->c);
}
} -
ah lol ^^
il suffit de faire
void AfficheMot(char *mot){
printf("%s \n",mot" );
} -
essai en mettant un espace entre " et %s
void AfficheMot(char *mot){
printf(" %s",mot" );
} -
non il ne doit pas prendre en compte les espace apres les % mais avant je pense qu'il les prend essai ca coute rien
-
-
attend j'ai bien compris tu veux afficher tes mot et des espaces entres chaque mot ?
essai
void AfficheMot(char *mot){
printf("%s",mot );
printf(" " );
}
sinon après je vois pas -
je peux te proposer
printf("%s \t",mot" );
mais ca va mettre plusieurs espace c'est peut être mieux que rien -
ya un probleme chez toi parce que moi quand je fais
#include <stdio.h>
#include <stdlib.h>
int main(void){
char* c ="blabla";
printf("%s ",c);
printf("%s",c);
system("pause");
return 0;
}
ca m'affiche "blabla blabla" -
c'est quoi ta config ?
t'es sous linux ? windows ?
32 , 64 bit ? -
c'est très très bizarre
et si tu fais ca ?
#include <stdio.h>
#include <stdlib.h>
int main(void){
char c[7] ="blabla";
printf("%s ",c);
printf("%s",c);
system("pause");
return 0;
} -
ya un probleme autre par dans ton code alors
colle le ( lisiblement )-
ok :) merci
#include<stdio.h> #include<stdlib.h> #include<string.h> #include "table_hash.h" #define TAILLEHASH 307 int main() { FILE *F; char mot[100]; int i; unsigned int cle,pt; int nl, pos; nl=pos=1; char c; int seuil; Liste **L; Liste **TableHash; TableHash = (Liste **) malloc (TAILLEHASH * sizeof(Liste *)); for(i=0;i<TAILLEHASH;++i) TableHash[i] = NULL; pt=hash_cle("."); //printf("%d",pt); printf("debut du programme \n----------------------------------\n"); F=fopen("C:\\Documents and Settings\\siril\\Bureau\\projetcyrine\\text.txt","r"); while(fscanf(F,"%s",mot)==1){ cle = hash_cle(mot); if ((cle!=pt ) && (! ChercherMotDansTableHash(TableHash,mot))) TableHash[cle] = InsertionEnTete(TableHash[cle],mot); } fclose(F ); F=fopen("C:\\Documents and Settings\\siril\\Bureau\\projetcyrine\\text.txt","r"); PosLigne(F,TableHash); //filtrage printf("\nla liste filtre :\n----------------------------------\n"); FiltreListe(TableHash,2); AfficherTableHash(TableHash); //géneration des 2seq Liste *seq=NULL; Generer2seq(TableHash, 2, &seq); printf("\nla liste des 2seq :\n----------------------------------\n"); AfficherListe(seq); //printf("Chercher mot : "); //scanf("%s",mot); //if(cle = ChercherMotDansTableHash(TableHash,mot)) // printf("%s existant dans le conteneur %u\n",mot,hash_cle(mot)); //else // printf("%s inexistant dans la table de hash\n"); scanf("%c",c); return 0; } void ParcourirElementTableHash(Liste **TableHash,char *mot){ int cle; Liste *p; cle = hash_cle(mot); for(p=TableHash[cle];p!=NULL;p=p->suivant) if(strcmp(mot,p->mot) == 0){ printf("%s : ",mot); AfficherCoordonnees(p->c); } } Liste *InsertionEnTete(Liste *L,char *mot){ Liste *nouveau; nouveau = (Liste *) malloc (sizeof(Liste)); strcpy(nouveau->mot,mot); nouveau->suivant = L; nouveau->c = NULL; return nouveau; } Coordonnees *InsertionEnTeteCoordonnee(Coordonnees *C,int nl ,int pos){ Coordonnees *nouveau; nouveau = (Coordonnees *) malloc (sizeof(Coordonnees)); nouveau->pos = pos; nouveau->nl = nl; nouveau->suivant = C; return nouveau; } void AfficherListe(Liste *L){ Liste *p; for(p=L;p!=NULL;p=p->suivant){ AfficheMot(p->mot); AfficherCoordonnees(p->c); } } void AfficherCoordonnees(Coordonnees *C){ Coordonnees *p; for(p=C;p!=NULL;p=p->suivant) printf(" (%d,%d) ",p->nl,p->pos); printf("\n"); } void AfficheMot(char *mot){ char *espace; espace =" "; printf("%s",mot ); printf("%s",espace); } unsigned int hash_cle(char *mot){ unsigned int val = 0; for(;*mot!='\0';++mot) val = *mot + 31 * val; return val % TAILLEHASH; } void AfficherTableHash(Liste **TableHash){ int i; for(i=0;i<TAILLEHASH;++i) if(TableHash[i] != NULL){ printf("Conteneur %d \n",i); AfficherListe(TableHash[i]); printf("----------------------------------\n"); } } unsigned int ChercherMotDansTableHash(Liste **TableHash,char *mot){ Liste *p; unsigned int cle; cle = hash_cle(mot); for(p=TableHash[cle];p!=NULL;p=p->suivant) if(strcmp(p->mot,mot)==0) return 1; return 0; } void FiltreListe(Liste **TableHash, int seuil){ Liste *p ,*last; Coordonnees *c; int i,supp=0,nb=0; for(i=0;i<TAILLEHASH;++i) if(TableHash[i] != NULL){ nb=0; last=TableHash[i]; for(p=TableHash[i];p!=NULL;p=p->suivant){ supp=0; nb++; for(c=p->c;c!=NULL;c=c->suivant) supp++; if( supp <seuil){ // mot n'est pas frequent if (last==p) // cas du première mot { TableHash[i]=p->suivant; last=p->suivant; } else last->suivant=p->suivant; nb--; }else last=p; } if (nb==0) TableHash[i] = NULL; } } void Generer2seq(Liste **TableHash, int seuil, Liste **seq){ Liste *p1, *p2, *ss,*t; Coordonnees *c1,*c2; int i,j,supp1=0,supp2=0,nb=0; char s1[50], s2[50],*t1,*t2; ss=*seq; printf("\nconstruction des 2seq \n---------------\n"); for(i=0;i<TAILLEHASH;++i) if(TableHash[i] != NULL) for(p1=TableHash[i];p1!=NULL;p1=p1->suivant) for(j=i;j<TAILLEHASH;++j) if(TableHash[j] != NULL){ if (j==i) t=p1; else t=TableHash[j]; for(p2=t;p2!=NULL;p2=p2->suivant){ if (strcmp(p1->mot,p2->mot)!=0){ // si 2 mots differents trouvés Liste *tmp1=NULL, *tmp2=NULL; strcpy(s1,p1->mot); strcpy(s2,p2->mot); t1=strcat(s1,p2->mot); t2=strcat(s2,p1->mot); tmp1=InsertionEnTete(tmp1,s1); tmp2=InsertionEnTete(tmp2,s2); printf("creation TMP1 et TMP2 : %s ,%s\n",s1,s2); supp1=0;supp2=0; for(c1=p1->c;c1!=NULL;c1=c1->suivant) // calcule de supp for(c2=p2->c;c2!=NULL;c2=c2->suivant){ if (c1->nl == c2->nl){ if(c1->pos < c2->pos){ tmp1->c=InsertionEnTeteCoordonnee(tmp1->c,c1->nl ,c2->pos); supp1++; } else{ tmp2->c=InsertionEnTeteCoordonnee(tmp2->c,c1->nl ,c1->pos); supp2++; } } } // fin calcul supp if(supp1>=seuil){ //printf("%d ",supp1); if (*seq==NULL) *seq=tmp1; else ss->suivant=tmp1; ss=tmp1; //printf("%s : OK1\n",tmp1->mot); } if(supp2>=seuil){ //printf("%d ",supp2); if (*seq==NULL) *seq=tmp2; else ss->suivant=tmp2; ss=tmp2; //printf("%s : OK2\n",tmp2->mot); } } // fin de traitement des mot //printf("fin"); } } printf("////fin calcule 2seq\n"); } void PosLigne(FILE *F,Liste **TableHash){ char s[50]; int nl,pos,i; Liste *p; nl=pos=1; while(fscanf(F,"%s",s)==1){ for(i=0;i<TAILLEHASH;++i) if(TableHash[i]!=NULL) for(p=TableHash[i];p!=NULL;p=p->suivant) if(strcmp(p->mot,s)==0) p->c=InsertionEnTeteCoordonnee(p->c,nl ,pos); if(fgetc(F)=='\n'){ ++nl; pos=0; } ++pos; } printf("\n"); }
table-hachage.h#ifndef TABLE_HASH #define TABLE_HASH typedef struct c{ int pos; int nl; struct c *suivant; }Coordonnees; typedef struct L{ char mot[50]; Coordonnees *c; struct L *suivant; }Liste; Liste *InsertionEnTete(Liste *L,char *mot); Coordonnees *InsertionEnTeteCoordonnee(Coordonnees *C,int nl,int pos); void AfficherListe(Liste *L); void AfficherCoordonnees(Coordonnees *C); void AfficheMot(char *mot); unsigned int hash_cle(char *mot); void AfficherTableHash(Liste **TableHash); unsigned int ChercherMotDansTableHash(Liste **TableHash,char *mot); void PosLigne(FILE *F,Liste **TableHash); void frequent( Liste **TableHash , int mc); void ParcourirElementTableHash(Liste **TableHash,char *mot); void FiltreListe(Liste **L, int seuil); void Generer2seq(Liste **TableHash, int seuil, Liste **seq); #endif
-
-
tu as essayé en declarant un char * au lieu de char[50] ?
-
ben essaye
typedef struct L{
char* mot;
Coordonnees *c;
struct L *suivant;
}Liste; -
derniere essaie
essai de faire tout dans la meme fonction
void AfficherListe(Liste *L){
Liste *p;
for(p=L;p!=NULL;p=p->suivant){
fprint(stderr,"%s ",p->mot);
AfficherCoordonnees(p->c);
}
}
- 1
- 2