Se connecter avec
S'enregistrer | Connectez-vous

Langage C

Dernière réponse : dans Programmation

Bonjours / Bonsoir à vous tous / toutes

Je suis débutant dans la programmation en langage C
et je voudrai savoir si quelqu'un pourrait m'aider ?

Mon but de mon "exercice" est de changer un caractère ( 'a') par un autre ('b')

Si vous pourriez m'indiquer quelque code ou votre avis, ou un truc qui pourrai me guider d'avantage.

Merci à vous

Brohs !


j'ai fais un code, mais celui le change comme je le souhaite, mais pas dans mon fichier texte, je ne sais pas comment faire pour que sa soit dans le fichier .txt, mais pour changer dans un fichier ce n'est pas :
int fputc(int caractere, FILE* pointeurSurFichier); ?
ou
int fputs(const char* chaine, FILE* pointeurSurFichier); ?
je vous fourni le code :

  1. #include <string.h>
  2. #include <stdio.h>
  3.  
  4. int CharToChar(char sString[], char sResult[], char cChar1, char cChar2)
  5. {
  6. int i = 0;
  7. int iLength = strlen(sString);
  8. int iResult = 0;
  9.  
  10. for(i = 0; i < iLength; i++)
  11. {
  12. sResult[i] = sString[i];
  13.  
  14. if(sString[i] == cChar1)
  15. {
  16. sResult[i] = cChar2;
  17. iResult = 1;
  18. }
  19. }
  20.  
  21. return iResult;
  22. }
  23.  
  24. {
  25. char sString[256] = "aazz4a8s91qs78s81t4rg8g15d4189s4aadz5dd94r1rz8a8";
  26. char cChar1 = 'a';
  27. char cChar2 = 'b';
  28. char sResult[256] = "";
  29. int iResult = 0;
  30.  
  31. printf("Phrase de base: %s\n", sString);
  32.  
  33. iResult = CharToChar(sString, sResult, cChar1, cChar2);
  34.  
  35. if(iResult == 0)
  36. {
  37. printf("Rien n'a change !\n");
  38. }
  39.  
  40. printf("Nouvelle phrase: %s\n", sResult);
  41.  
  42. return 0;
  43. }

Autres pages sur : langage

Lassé par la pub ? Créez un compte

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <Windows.h>
  4.  
  5. #define TAILLE_MAX 10000
  6.  
  7. char* str_replace (const char* txt, const char* Avant, const char* Apres)
  8. {
  9. const char* pos;
  10. char* TxtRetour;
  11. size_t PosTxtRetour;
  12.  
  13. size_t Long;
  14. size_t TailleAllouee;
  15.  
  16. pos = strstr (txt,Avant);
  17.  
  18. if (pos == NULL) return NULL;
  19.  
  20. Long = (size_t)pos -(size_t)txt;
  21. TailleAllouee = Long +strlen(Apres)+1;
  22. TxtRetour = (char *)malloc(TailleAllouee);
  23. PosTxtRetour = 0;
  24.  
  25. strncpy (TxtRetour+PosTxtRetour,txt,Long); PosTxtRetour += Long;
  26. txt = pos+strlen(Avant);
  27.  
  28. Long = strlen(Apres);
  29. strncpy (TxtRetour+PosTxtRetour,Apres,Long); PosTxtRetour += Long;
  30.  
  31. pos = strstr (txt,Avant);
  32. while (pos != NULL)
  33. {
  34. Long = (size_t)pos -(size_t)txt;
  35. TailleAllouee += Long+strlen(Apres);
  36. TxtRetour = (char *)realloc(TxtRetour,TailleAllouee);
  37.  
  38. strncpy (TxtRetour+PosTxtRetour,txt,Long); PosTxtRetour += Long;
  39.  
  40. txt = pos+strlen(Avant);
  41.  
  42. Long = strlen(Apres);
  43. strncpy (TxtRetour+PosTxtRetour,Apres,Long); PosTxtRetour += Long;
  44.  
  45. pos = strstr (txt,Avant);
  46. }
  47. Long = strlen(txt)+1;
  48. TailleAllouee += Long;
  49. TxtRetour = (char*)realloc(TxtRetour,TailleAllouee);
  50. strncpy (TxtRetour+PosTxtRetour,txt,Long);
  51. return TxtRetour;
  52. }
  53.  
  54. int main()
  55. {
  56.  
  57. FILE* fileTest = NULL;
  58. char buffer[TAILLE_MAX] = "", bufferDeux[10000] = "";
  59.  
  60.  
  61. fileTest = fopen("test.txt", "r");
  62.  
  63. if (fileTest != NULL)
  64. {
  65. while (fgets(buffer, TAILLE_MAX, fileTest) != NULL)
  66. {
  67. strcat(bufferDeux,buffer);
  68. }
  69. fclose(fileTest);
  70.  
  71. printf("Chaine non modifier : %s\r\n", bufferDeux);
  72. char *chaineModif = str_replace(bufferDeux,"a","b");
  73. printf("Chaine non modifier : %s\r\n", chaineModif);
  74.  
  75.  
  76. fileTest = fopen("test.txt", "w+");
  77.  
  78. if (fileTest != NULL)
  79. {
  80. fputs(chaineModif, fileTest);
  81. fclose(fileTest);
  82. }
  83. }
  84.  
  85.  
  86. system("PAUSE");
  87. return 0;
  88. }



Voila, Si tu a besoin que je mette des commentaire dis moi le.

Voici les petits soucie rencontrer lors de la compilation.

test.c: In function ‘str_replace’:
test.c:17: warning: incompatible implicit declaration of built-in function ‘malloc’
test.c:28: warning: incompatible implicit declaration of built-in function ‘realloc’
test.c: warning: incompatible implicit declaration of built-in function ‘realloc’
Lassé par la pub ? Créez un compte
Tom's guide dans le monde