str.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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* trim(char* str){
  33. return ltrim(rtrim(str, ' '), ' ');
  34. }
  35. char* mtrim(char* str, char mask){
  36. return ltrim(rtrim(str, mask), mask);
  37. }
  38. char* ltrim(char* str, char mask){
  39. //Variable
  40. int cmpt = 0;
  41. char* res;
  42. //Compte le nombre d'espace
  43. while(str[cmpt] == mask){
  44. cmpt++;
  45. }
  46. //Si aucun espace au debut
  47. if(cmpt == 0){
  48. res = malloc(sizeof(char) * (strlen(str) + 1));
  49. memset(res, 0, strlen(str) + 1);
  50. strcpy(res, str);
  51. return res;
  52. }
  53. //Sinon creation nouvelle chaine
  54. res = malloc(sizeof(char) * (strlen(str) - cmpt + 1));
  55. memset(res, 0, strlen(str) - cmpt + 1);
  56. for(int i = 0, j = cmpt; j < (strlen(str)); i++, j++){
  57. res[i] = str[j];
  58. }
  59. //Retour nouvelle chaine
  60. return res;
  61. }
  62. char* rtrim(char* str, char mask){
  63. //Variable
  64. int cmpt = strlen(str) - 1;
  65. char* res;
  66. //Compte le nombre d'espace
  67. while(str[cmpt] == mask){
  68. cmpt--;
  69. }
  70. //Si aucun espace au debut
  71. if(cmpt == strlen(str) - 1){
  72. res = malloc(sizeof(char) * (strlen(str) + 1));
  73. memset(res, 0, strlen(str) + 1);
  74. strcpy(res, str);
  75. return res;
  76. }
  77. cmpt++;
  78. //Sinon creation nouvelle chaine
  79. res = malloc(sizeof(char) * (cmpt + 2));
  80. memset(res, 0, cmpt + 2);
  81. for(int i = 0; i < cmpt; i++){
  82. res[i] = str[i];
  83. }
  84. //Retour nouvelle chaine
  85. return res;
  86. }
  87. void reverse(char *str, int len){
  88. int i=0, j=len-1, temp;
  89. while (i<j)
  90. {
  91. temp = str[i];
  92. str[i] = str[j];
  93. str[j] = temp;
  94. i++; j--;
  95. }
  96. }
  97. void ftoa(float n, char *res, int afterpoint) {
  98. // Extract integer part
  99. int ipart = (int)n;
  100. // Extract floating part
  101. float fpart = n - (float)ipart;
  102. // convert integer part to string
  103. int i = intToStr(ipart, res, 0);
  104. // check for display option after point
  105. if (afterpoint != 0)
  106. {
  107. res[i] = '.'; // add dot
  108. // Get the value of fraction part upto given no.
  109. // of points after dot. The third parameter is needed
  110. // to handle cases like 233.007
  111. fpart = fpart * pow(10, afterpoint);
  112. intToStr((int)fpart, res + i + 1, afterpoint);
  113. }
  114. }