Advertisement
andy_shev

num-replace.c

Feb 21st, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. #include <errno.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #if defined(TEST_1)
  8. const char *input = "1str, 123 gdg1(75); test32 0";
  9. #elif defined(TEST_2)
  10. const char *input = "1 str, 123 gdg1(75); test32 0";
  11. #elif defined(TEST_3)
  12. const char *input = "str, 123 gdg1(75); test32 0s";
  13. #elif defined(TEST_4)
  14. const char *input = "str, 123 gdg1(75); test32 0 s";
  15. #elif defined(TEST_5)
  16. const char *input = "str, 123 gdg1(75); test32 1d1 0";
  17. #elif defined(TEST_6)
  18. const char *input = "str, 123 gdg1(75); test32 d1d 0";
  19. #elif defined(TEST_7)
  20. const char *input = "1 123 1 75 3 1";
  21. #elif defined(TEST_8)
  22. const char *input = "a bcd e fg h j";
  23. #elif defined(TEST_9)
  24. const char *input = "a";
  25. #elif defined(TEST_10)
  26. const char *input = "0";
  27. #else
  28. const char *input = "str, 123 gdg1(75); test32 0";
  29. #endif
  30.  
  31. const char *RE = "NUM";
  32.  
  33. static char *put_replacement(char *out, const char *r, size_t len)
  34. {
  35.     memcpy(out, r, len);
  36.     return out + len;
  37. }
  38.  
  39. static void replace(const char *in, char *out, size_t outlen)
  40. {
  41.     const char *q;
  42.     int flag = 0;
  43.     char p;
  44.  
  45.     while ((p = *in++)) {
  46.         if (flag == 0) {
  47.             if (isdigit(p)) {
  48.                 q = in - 1;
  49.                 flag = 1;
  50.                 continue;
  51.             }
  52.             if (isalpha(p))
  53.                 flag = 2;
  54.         }
  55.  
  56.         if (flag == 1) {
  57.             if (isdigit(p))
  58.                 continue;
  59.             if (isalpha(p)) {
  60.                 out = put_replacement(out, q, in - 1 - q);
  61.                 flag = 2;
  62.             } else {
  63.                 out = put_replacement(out, RE, strlen(RE));
  64.                 flag = 0;
  65.             }
  66.         }
  67.  
  68.         if (flag == 2 && !(isalpha(p) || isdigit(p))) {
  69.             flag = 0;
  70.         }
  71.  
  72.         *out++ = p;
  73.     }
  74.  
  75.     if (flag == 1)
  76.         out = put_replacement(out, RE, strlen(RE));
  77.  
  78.     *out++ = '\0';
  79. }
  80.  
  81. int main(int argc, char *argv[])
  82. {
  83.     const char *in = input;
  84.     size_t outlen;
  85.     char *out;
  86.  
  87.     /*
  88.      * New length is counted as old * strlen(RE) / 2 + strlen(RE) since we
  89.      * have to have delimiters. Additional strlen(RE) for the case, when
  90.      * initial length is too small. And 1 for '\0' terminator.
  91.      */
  92.     outlen = strlen(in) * strlen(RE) / 2 + strlen(RE) + 1;
  93.     out = malloc(outlen);
  94.     if (!out)
  95.         return ENOMEM;
  96.  
  97.     replace(in, out, outlen);
  98.  
  99.     printf("in: %s\n", in);
  100.     printf("out: %s\n", out);
  101.  
  102.     free(out);
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement