str.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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* new_string(int length){
  33. char* str = malloc(sizeof(char) * (length + 1));
  34. memset(str, 0, length + 1);
  35. return str;
  36. }
  37. char* string_copy(char* src){
  38. int length = strlen(src);
  39. char* dest = new_string(length);
  40. strncpy(dest, src, length);
  41. return dest;
  42. }
  43. char* trim(char* str){
  44. return ltrim(rtrim(str, ' '), ' ');
  45. }
  46. char* mtrim(char* str, char mask){
  47. return ltrim(rtrim(str, mask), mask);
  48. }
  49. char* ltrim(char* str, char mask){
  50. //Variable
  51. int cmpt = 0;
  52. char* res;
  53. //Compte le nombre d'espace
  54. while(str[cmpt] == mask){
  55. cmpt++;
  56. }
  57. //Si aucun espace au debut
  58. if(cmpt == 0){
  59. res = malloc(sizeof(char) * (strlen(str) + 1));
  60. memset(res, 0, strlen(str) + 1);
  61. strcpy(res, str);
  62. return res;
  63. }
  64. //Sinon creation nouvelle chaine
  65. res = malloc(sizeof(char) * (strlen(str) - cmpt + 1));
  66. memset(res, 0, strlen(str) - cmpt + 1);
  67. for(int i = 0, j = cmpt; j < (strlen(str)); i++, j++){
  68. res[i] = str[j];
  69. }
  70. //Retour nouvelle chaine
  71. return res;
  72. }
  73. char* rtrim(char* str, char mask){
  74. //Variable
  75. int cmpt = strlen(str) - 1;
  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 == strlen(str) - 1){
  83. res = malloc(sizeof(char) * (strlen(str) + 1));
  84. memset(res, 0, strlen(str) + 1);
  85. strcpy(res, str);
  86. return res;
  87. }
  88. cmpt++;
  89. //Sinon creation nouvelle chaine
  90. res = malloc(sizeof(char) * (cmpt + 2));
  91. memset(res, 0, cmpt + 2);
  92. for(int i = 0; i < cmpt; i++){
  93. res[i] = str[i];
  94. }
  95. //Retour nouvelle chaine
  96. return res;
  97. }
  98. void reverse(char *str, int len){
  99. int i=0, j=len-1, temp;
  100. while (i<j)
  101. {
  102. temp = str[i];
  103. str[i] = str[j];
  104. str[j] = temp;
  105. i++; j--;
  106. }
  107. }
  108. void ftoa(float n, char *res, int afterpoint) {
  109. // Extract integer part
  110. int ipart = (int)n;
  111. // Extract floating part
  112. float fpart = n - (float)ipart;
  113. // convert integer part to string
  114. int i = intToStr(ipart, res, 0);
  115. // check for display option after point
  116. if (afterpoint != 0)
  117. {
  118. res[i] = '.'; // add dot
  119. // Get the value of fraction part upto given no.
  120. // of points after dot. The third parameter is needed
  121. // to handle cases like 233.007
  122. fpart = fpart * pow(10, afterpoint);
  123. intToStr((int)fpart, res + i + 1, afterpoint);
  124. }
  125. }
  126. char* remove_char(char* src, char carac){
  127. int length, compteur = 0;
  128. char* tmp = src;
  129. char* str;
  130. //Compte le nombre de fois ou le caracère apparait
  131. while(*tmp){
  132. if(*tmp == carac){
  133. compteur++;
  134. }
  135. tmp++;
  136. }
  137. //Creation nouvelle chaine
  138. length = strlen(src) - compteur;
  139. str = new_string(length);
  140. tmp = str;
  141. //Copie la chaine
  142. while(*src){
  143. if(*src != carac){
  144. *tmp = *src;
  145. tmp++;
  146. }
  147. src++;
  148. }
  149. return str;
  150. }