FONCTION STRCAT

Résolu/Fermé
CoDForlan Messages postés 44 Date d'inscription mercredi 17 juin 2020 Statut Membre Dernière intervention 21 juin 2023 - Modifié le 21 août 2020 à 02:51
CoDForlan Messages postés 44 Date d'inscription mercredi 17 juin 2020 Statut Membre Dernière intervention 21 juin 2023 - 21 août 2020 à 11:15
Bonjour,
Je débute dans la programmation du langage C et je suis rendu à l’apprentissage des chaînes de caractères str...
Donc pour m’entraîner je conçois des petites fonction qui n'ont pas vraiment de sens.
Et la j'ai un problème avec la fonction strcat qui permet je crois de concaténer une chaîne dans une autre et je n'y arrive pas.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int findNum(char* tabEntCalc, char* outEntCalc, int result, int numOfNum);


int main(int argc, char const* argv[]) {
 char tabEntCalc[256] = { 0 }; // je met par exempe 17 + 13
 char outEntCalc[256] = { 0 };
 int numOfNum = 0;
 int result = 0;
 findNum(tabEntCalc, outEntCalc, result, numOfNum);
 return 0;
}

int findNum(char* tabEntCalc, char* outEntCalc, int result, int numOfNum) {
 gets(tabEntCalc);
 numOfNum = strlen(tabEntCalc);
 for (int i = 0; i < numOfNum; ++i) {
  if (tabEntCalc[i] =='0' || tabEntCalc[i] == '1' || tabEntCalc[i] == '2' || tabEntCalc[i] == '3' || tabEntCalc[i] == '4' || tabEntCalc[i] == '5' || tabEntCalc[i] == '6' || tabEntCalc[i] == '7' || tabEntCalc[i] == '8' || tabEntCalc[i] == '9') {
   printf("0\n");
   //Je veux ajouter un 0 quand il y un chiffre
   strcat(outEntCalc, "0");
  }
  else {
   printf("1\n");
   //Je veux ajouter un 1 quand il y un chiffre
   strcat(outEntCalc, "1");
  }

 }
 printf("Le calcul         : %s\n", tabEntCalc);
 printf("Le resultat       : %d\n", result);
 printf("Sortie tabEntCalc : %d\n", outEntCalc);//pour afficher le résultat de strcat
 printf("Votre calcul fait %d caracteres\n", strlen(tabEntCalc));
}



Je veux ajouter un 0 quand il y un chiffre et ajouter un 1 quand il y a autre chose.

Le problème c'est que quand je test il me répond ça :

17+13
0
0
1
0
0
Le calcul : 17+13
Le resultat : 0
Sortie tabEntCalc : 6421784 <--------(comme si elle n'était pas initialisée ?)
Votre calcul fait 5 caracteres
A voir également:

1 réponse

yg_be Messages postés 22724 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 25 avril 2024 1 476
21 août 2020 à 09:20
bonjour, examine bien ce que tu fais en ligne 35. au besoin, compare avec la ligne 33.
0
CoDForlan Messages postés 44 Date d'inscription mercredi 17 juin 2020 Statut Membre Dernière intervention 21 juin 2023 1
Modifié le 21 août 2020 à 11:15
Merci pour ta réponse.
Le pire c'est que j'avais fais la même erreur 1 jour plus tôt. x)
0