Je suis très nouveau tampon à la programmation c, et pour une mission de l'école, je dois écrire un programm qui prend une chaîne en entrée et ajouter les lettres « ay » pour chaque mot qui commence par une consonne au début de ce mot. Il est suggéré que ceci devrait être fait avec strncpy et strcat.ajoutant chaîne à tableau de caractères à la position spécifique donnant débordement dans c la programmation
c'est le code que j'ai écrit:
#include <stdio.h>
#include <string.h>
int main()
{
char myString[50];
char abc[26] = "bcdfghjklmnpqrstvwxyz";
char strA[50];
char strB[50];
char strC[150];
char ay[3] = "ay";
printf("** Welcome to the Double Dutch game **\nPlease enter a string: ");
scanf(" %[^\n]s", &myString);
int i, j;
for (i=0; myString[i]!='0'; i++) {
for(j=0; abc[j]!='\0'; j++) {
if(abc[j] == myString[i]){
if(myString[i-1] == ' '){
strncpy(strC, &myString[0], i);
strncpy(strB, &myString[i], 40);
strcat(strC, ay);
strcat(strC, strB);
myString[0] = '\0';
strcat(myString, strC);
strC[0] = '\0';
strB[0] = '\0';
}
}
}
}
printf("Result: %s", myString);
}
Quand je lance ce code, il continue à donner l'erreur * Tampon overlow détecté *: /home/a.out terminé. Je ne peux pas trouver l'erreur que j'ai faite. J'espère que quelqu'un peut m'aider. Merci
Modifier 'char abc [26] = "bcdfghjklmnpqrstvwxyz", 'à' char abc [ ] = "bcdfghjklmnpqrstvwxyz"; '- Laissez le compilateur faire le travail des jambes –
' scanf ("% [^ \ n] s", & myString); '-> retirer la' et ' –
Qu'advient-il si le premier caractère dans la chaîne est une consonne? 'i' sera 0 et ce' if (myString [i-1] == '') 'check va sortir des limites. –