str.c 4.1 KB

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