Gallad

TP2_C_3

Jan 1st, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. /* Exercice 3
  2.  
  3.  
  4. Dans un tableau : |a|b|a|d|e|f|...|z|\0|
  5. Dans un autre stat :| 2 | 1 | 0 | 1 | 1 |...| |
  6. a b c d e ... z
  7. 0 1 2 3 4 ... 25
  8.  
  9. Si je tape 'a' -> code ASCII = 65 -> quel est son indice tableau stat ? -> 0 (case n°0)
  10. On va faire un décompte d'effectif de lettre qu'un utilisateur entre.
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. /* Boucle Lecture Chaine (var i)
  17. car = tolower(tab[i]);
  18. si((car>='a')&&(car<='z')){
  19. indice = car-'a'
  20. Stat[indice]++;
  21. }sinon{
  22. stat[26];
  23. }
  24. stat[27]++;
  25. finboucle
  26.  
  27. (int*)malloc(28*sizeof(int))
  28. */
  29.  
  30. char * saisie()
  31. {
  32. char tmp[1000];//1
  33. char *tab;
  34. int taille, i;
  35. saisieabis((tmp));//2
  36. taille=strlen(tmp)+1;//3
  37. tab = (char*)malloc(taille*sizeof(char));//4
  38. strcpy(tab, tmp);//5
  39. return tab;
  40.  
  41. }
  42.  
  43. int*fstat(char*tab){
  44. int i, *stat, ind;
  45. char car;
  46. stat=(int*)malloc(28*sizeof(int));
  47. for(i=0;i<28;i++){ // Initialisation du tableau à coups de 0
  48. stat[i]=0;
  49. }
  50. for(i=0;tab[i]<28;i++){
  51. car=tolower(tab[i]); // remplace toutes les lettres par des miniscules
  52. if((car>'a')&&(car<='z')){ // si le carac est compris entre a et z
  53. ind=car-'a';
  54. stat[ind]++;
  55. }else{
  56. stat[26]++;
  57. }
  58. }
  59. stat[27]=i;
  60. return stat;
  61. }
  62.  
  63. void affichestat(int*stat){
  64. int i;
  65. for(i=0;i<26;i++){
  66. if(stat[i]!=0){ // Si 0 occurence d'un carac
  67. printf("il y a %d %c.\n",stat[i],i+'a');
  68. }
  69. }
  70. printf("il y a %d caractere speciaux et %d caracteres en tout", stat[26],stat[27]);
  71. }
  72.  
  73. int main(){
  74. char*tab;
  75. int*stat;
  76. tab=saisie();
  77.  
  78. }
  79.  
  80.  
  81.  
  82.  
  83. //________________________________________________________________________________________
  84. void saisieabis (char*tab)
  85. {
  86. char c, * p;
  87. p = tab;
  88. do
  89. {
  90. c=getchar();
  91. *p=c;
  92. p++; // p = p+1;
  93. }
  94. while ( *(p-1) != '\n');
  95. *(p-1) = '\0'; //// *--p = '\0';
  96. }
  97.  
  98. //___________________________________________________________________________________________________
  99. int * fstat ( char* tab)
  100. {
  101. int i, *stat, ind;
  102. char caractere;
  103. stat = (int*) malloc (28 * sizeof(int));// ici on donne la place pour le tableau
  104. for (i = 0; i <= 27; i++) //initialisation du tableau
  105. {
  106. stat[i] = 0;//avec des 0
  107. }
  108. for (i = 0; tab[i]!='\0'; i++)
  109.  
  110. {
  111. caractere = tolower(tab[i]);// ici on gere uniquement des minuscules
  112. if ((caractere>='a') && (caractere<='z')) // puis on enleve le code ASCII de A
  113. {
  114. ind = caractere - 'a';
  115. stat[ind]++;// Et on incrémente l indice
  116. }
  117. else
  118. {
  119. stat[26] ++;// Si ce n est pas un caractere alors on augmente le compteur de caractere spéciaux
  120. }
  121. stat[27] ++; //puis on incrémente la valeur du compteur de caractere total
  122.  
  123. }
  124. return stat;
  125. }
  126.  
  127. //_________________________________________________________________________________________________________________
  128. void affichestat(int * stat)
  129. {
  130. int i;
  131. for ( i = 0; i < 26; i++)
  132. {
  133. if ( stat[i] != 0)
  134. {
  135. printf("il y a %d %c \n", stat[i], i+'a' );
  136. }
  137.  
  138. }
  139. printf("il y a %d car spéciale et %d caractere totaux", stat[26], stat[27]);
  140. }
  141. //__________________________________________________________________________________________________________________
  142. int main()
  143. {
  144. char * tab;
  145. int * stat;
  146. printf("Entre une chaine de caractere\n");
  147. tab = saisieb();
  148. stat = fstat (tab);
  149.  
  150. affichestat(stat);
  151. return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment