str.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * File: str.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 12 octobre 2018
  6. */
  7. #define _POSIX_C_SOURCE 200112L
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include "str.h"
  12. /* --- Fonctions privées --- */
  13. // Converts a given integer x to string str[]. d is the number
  14. // of digits required in output. If d is more than the number
  15. // of digits in x, then 0s are added at the beginning.
  16. int intToStr(int x, char str[], int d){
  17. int i = 0;
  18. while (x)
  19. {
  20. str[i++] = (x%10) + '0';
  21. x = x/10;
  22. }
  23. // If number of digits required is more, then
  24. // add 0s at the beginning
  25. while (i < d)
  26. str[i++] = '0';
  27. reverse(str, i);
  28. str[i] = '\0';
  29. return i;
  30. }
  31. /* --- Fonctions publiques --- */
  32. char** str_split(char* str, const char delim, int* length) {
  33. char** res;
  34. char* signet, * tmp = str;
  35. char last = 0;
  36. int compteur = 0, nb = 1, taille, count = 0;
  37. //Compte le nombre d'element
  38. while (*tmp) {
  39. if (*tmp == delim && last != delim) {
  40. nb++;
  41. }
  42. last = *tmp;
  43. tmp++;
  44. }
  45. //Creation du tableau
  46. res = malloc(sizeof (char*));
  47. //Decoupage
  48. tmp = str;
  49. while (*tmp) {
  50. //Si c'est le dernier mot
  51. if (compteur == nb - 1) {
  52. //Ajoute tous ce qui reste
  53. res[compteur] = malloc(sizeof (char) * (strlen(str) - count));
  54. int i = 0;
  55. while(*tmp){
  56. res[compteur][i++] = *tmp;
  57. tmp++;
  58. }
  59. } else {
  60. //Recup la taille du mot
  61. signet = tmp;
  62. taille = 0;
  63. while (*tmp != delim) {
  64. taille++;
  65. tmp++;
  66. count++;
  67. }
  68. //Creation du mot
  69. res[compteur] = malloc(sizeof (char) * taille);
  70. //Ajout du mot
  71. for (int i = 0; i < taille; i++) {
  72. res[compteur][i] = *signet;
  73. signet++;
  74. }
  75. compteur++;
  76. //Passe les delimiteurs consecutif
  77. while (*tmp == delim) {
  78. tmp++;
  79. count++;
  80. }
  81. }
  82. }
  83. //Retour nombre de mot et tableau
  84. *length = nb;
  85. return res;
  86. }
  87. char* trim(char* str){
  88. return ltrim(rtrim(str, ' '), ' ');
  89. }
  90. char* mtrim(char* str, char mask){
  91. return ltrim(rtrim(str, mask), mask);
  92. }
  93. char* ltrim(char* str, char mask){
  94. //Variable
  95. int cmpt = 0;
  96. char* res;
  97. //Compte le nombre d'espace
  98. while(str[cmpt] == mask){
  99. cmpt++;
  100. }
  101. //Si aucun espace au debut
  102. if(cmpt == 0){
  103. return str;
  104. }
  105. //Sinon creation nouvelle chaine
  106. res = malloc(sizeof(char) * (strlen(str) - cmpt + 1));
  107. for(int i = 0, j = cmpt; i < (strlen(str) - cmpt); i++, j++){
  108. res[i] = str[j];
  109. }
  110. //Retour nouvelle chaine
  111. return res;
  112. }
  113. char* rtrim(char* str, char mask){
  114. //Variable
  115. int cmpt = strlen(str) - 1;
  116. char* res;
  117. //Compte le nombre d'espace
  118. while(str[cmpt] == mask){
  119. cmpt--;
  120. }
  121. //Si aucun espace au debut
  122. if(cmpt == strlen(str) - 1){
  123. return str;
  124. }
  125. cmpt++;
  126. //Sinon creation nouvelle chaine
  127. res = malloc(sizeof(char) * (cmpt + 2));
  128. for(int i = 0; i < cmpt; i++){
  129. res[i] = str[i];
  130. }
  131. //Retour nouvelle chaine
  132. return res;
  133. }
  134. void reverse(char *str, int len){
  135. int i=0, j=len-1, temp;
  136. while (i<j)
  137. {
  138. temp = str[i];
  139. str[i] = str[j];
  140. str[j] = temp;
  141. i++; j--;
  142. }
  143. }
  144. void ftoa(float n, char *res, int afterpoint) {
  145. // Extract integer part
  146. int ipart = (int)n;
  147. // Extract floating part
  148. float fpart = n - (float)ipart;
  149. // convert integer part to string
  150. int i = intToStr(ipart, res, 0);
  151. // check for display option after point
  152. if (afterpoint != 0)
  153. {
  154. res[i] = '.'; // add dot
  155. // Get the value of fraction part upto given no.
  156. // of points after dot. The third parameter is needed
  157. // to handle cases like 233.007
  158. fpart = fpart * pow(10, afterpoint);
  159. intToStr((int)fpart, res + i + 1, afterpoint);
  160. }
  161. }