int AjusterTailleChaine(char * Chaine, long PositionAjustement, long NDeplacement) { if ((Chaine[0]=='\0')||(PositionAjustement<0)) { return -1; } if (NDeplacement==0) { return 0; } //Cas seulement négatif long LongueurChaine=strlen(Chaine); NDeplacement=abs(NDeplacement); while(PositionAjustement+NDeplacement<=LongueurChaine) { Chaine[PositionAjustement]=Chaine[PositionAjustement+NDeplacement]; PositionAjustement++; } }
Chaine[IndiceGauche]= Chaine[IndiceDroit];
char * Phrase = "Bonjour a tous"; Phrase[8]= Phrase[13]; // <=== l'erreur est ici !
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionchar Phrase[] = "Bonjour a tous";
int AjusterTailleChaine(char Chaine[], long PositionAjustement, long NDeplacement) { if ((Chaine[0]=='\0')||(PositionAjustement<0)) { return -1; } if (NDeplacement==0) { return 0; } long LongChaine=LongueurChaine(Chaine); long IndiceGauche; long IndiceDroit; if (NDeplacement<0) { IndiceDroit = PositionAjustement + abs(NDeplacement); IndiceGauche = PositionAjustement ; while(Chaine[IndiceDroit] != '\0') { Chaine[IndiceGauche]= Chaine[IndiceDroit]; IndiceGauche= IndiceGauche + 1; IndiceDroit= IndiceDroit + 1; } Chaine[IndiceGauche]= '\0';// Refermer la chaine par un EOS LongChaine= LongueurChaine(Chaine); Chaine = (char *) malloc(LongChaine); return 0; } if (NDeplacement>0) { IndiceGauche= PositionAjustement; IndiceDroit = PositionAjustement + NDeplacement ; LongChaine= LongueurChaine(Chaine); Chaine = (char *) malloc(LongChaine + NDeplacement); while(Chaine[IndiceGauche] != '\0') { Chaine[IndiceDroit]= Chaine[IndiceGauche]; if ((IndiceGauche>=PositionAjustement)&&(IndiceGauche<=PositionAjustement+NDeplacement)) { Chaine[IndiceGauche]= ' '; } IndiceDroit++; IndiceGauche++; } Chaine[IndiceGauche]= '\0'; } }
char *AjusterTailleChaine(...) { char *s = malloc(LongChaine + NDeplacement + 1); // +1 pour le 0 final if ( "YaErreur" ) return (char *)0; // on dit aussi "null" ...copier comme il faut... return s; }
char * AjusterTailleChaine(char * Chaine, long PositionAjustement, long NDeplacement) { if ((Chaine[0]=='\0')||(PositionAjustement<0)) { return Chaine; } if (NDeplacement==0) { return Chaine; } long LongChaine=LongueurChaine(Chaine); long IndiceGauche; long IndiceDroit; char* chaineCopie; if (NDeplacement<0) { IndiceDroit = 0; IndiceGauche = 0 ; chaineCopie = (char*) malloc(((LongueurChaine(Chaine) + NDeplacement) * sizeof(char))); while(Chaine[IndiceDroit] != '\0') { if((IndiceDroit=PositionAjustement-NDeplacement)) { chaineCopie[IndiceGauche]= Chaine[IndiceDroit]; IndiceGauche= IndiceGauche + 1; } IndiceDroit= IndiceDroit + 1; } chaineCopie[IndiceGauche]= '\0';//Chaine[IndiceDroit]; // Refermer la chaine par un EOS return chaineCopie ; } if (NDeplacement>0) { int Taille = LongueurChaine(Chaine)-1; chaineCopie = (char*) malloc((Taille + NDeplacement) * sizeof(char)); int Indice= Taille; strcpy(chaineCopie, Chaine); chaineCopie[Indice + NDeplacement + 1] = 0; while(Indice >= PositionAjustement ) { chaineCopie[Indice + NDeplacement] = Chaine[Indice]; Indice--; } for(int i = 0; i < NDeplacement ; i++) { chaineCopie[PositionAjustement + i] = ' '; } return chaineCopie; } }
int Taille = LongueurChaine(Chaine)-1;Pourquoi -1 ?
chaineCopie = (char*) malloc(((LongueurChaine(Chaine) + NDeplacement) * sizeof(char)));
chaineCopie = (char*) malloc((LongChaine + NDeplacement + 1) * sizeof(char));