Guest User

Untitled

a guest
Aug 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. Deleting characters from C string
  2. void convertStr(char *analysisBuffer)
  3. {
  4. char *exp;
  5.  
  6. if( (exp = strstr(analysisBuffer,"x+"))!=NULL||(exp = strstr(analysisBuffer,"X+"))!= NULL)
  7. {
  8. exp += 2;
  9. char * zeroIt = exp;
  10. while(*zeroIt == '0')
  11. ++zeroIt;
  12. unsigned int x = exp - analysisBuffer;
  13. analysisBuffer[x] = '';
  14. strcat(analysisBuffer,zeroIt);
  15. }
  16. }
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21.  
  22. char* strip_zero(char* str, const char* startseq) {
  23. // while there is an occurence of the substring
  24. size_t ssize = strlen(startseq);
  25. size_t oldlen = strlen(str) + 1; // account for terminator
  26. size_t rems = 0;
  27. char* begin = str;
  28.  
  29. while((begin = strstr(begin, startseq))) {
  30. // move to the end of the sequence
  31. begin += ssize;
  32. char* walk = begin;
  33. // walk until we reach nonzero
  34. while(*walk == '0') { ++walk; ++rems; }
  35. // move the string forward
  36. memmove(begin, walk, strlen(walk) + 1);
  37. }
  38.  
  39. // realloc the string
  40. return (char*)realloc(str, oldlen - rems);
  41. }
  42.  
  43. int main(void)
  44. {
  45. // make a copy so we can modify
  46. const char* a = "x+20.301x+00000x+0005x+";
  47. char* foo = (char*)malloc(strlen(a) + 1);
  48. strcpy(foo, a);
  49. printf("%s n", foo);
  50. foo = strip_zero(foo, "x+");
  51. printf("%s n", foo);
  52. free(foo);
  53. return 0;
  54. }
  55.  
  56. #define _GNU_SOURCE
  57. #include <stdlib.h>
  58. #include <stdio.h>
  59. #include <string.h>
  60.  
  61. void
  62. convertStr(char *analysisBuffer)
  63. {
  64. char *exp = strcasestr(analysisBuffer, "x+");
  65.  
  66. if (exp) {
  67. exp += 2;
  68. if (*exp == '0') {
  69. while (*exp == '0') {
  70. *exp = '';
  71. exp++;
  72. }
  73. strcat(analysisBuffer, exp);
  74. }
  75. }
  76. }
  77.  
  78. int
  79. main(int argc, char *argv[])
  80. {
  81. char test[] = "20.301X+0005";
  82.  
  83. printf("before: %sn", test);
  84. convertStr(test);
  85. printf("after : %sn", test);
  86.  
  87. return EXIT_SUCCESS;
  88. }
  89.  
  90. char * write = str;
  91. char * read = str;
  92. while(*read){
  93. while(*read && *read == '0'){ read++; }
  94. *write++ = *read++;
  95. }
  96. *write = '';
  97.  
  98. unsigned int x = exp - analysisBuffer;
  99. analysisBuffer[x] = '';
  100. strcat(analysisBuffer,zeroIt);
  101.  
  102. memmove(exp, zeroIt, strlen(zeroIt) + 1);
  103.  
  104. size_t strip0( char *buff);
  105. size_t strip0( char *buff)
  106. {
  107. size_t src,dst;
  108.  
  109. for (src=dst=0; buff[dst] = buff[src++] ;dst++ ) {
  110. if ((buff[dst] == 'x' || buff[dst] == 'X') && buff[src] == '+') {
  111. buff[++dst] = buff[src++] ;
  112. while (buff[src] == '0') src++;
  113. }
  114. }
  115. return dst;
  116. }
  117.  
  118. #include <stdio.h>
  119.  
  120. int main(int arc, char **argv)
  121. {
  122. char data[] = "100x+003 aap 200x+300 100X+003 noot 200X+300 mies 1234xX+000000000zzz";
  123.  
  124. printf("Org: %sn", data);
  125. strip0(data);
  126. printf("New: %sn", data);
  127.  
  128. return 0;
  129. }
Add Comment
Please, Sign In to add comment