Advertisement
dmilicev

remove_the_parentheses.c

Mar 13th, 2022
843
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     remove_the_parentheses.c
  4.  
  5. https://www.codewars.com/kata/5f7c38eb54307c002a2b8cc8
  6. https://www.codewars.com/kata/5f7c38eb54307c002a2b8cc8/train/c
  7. https://www.codewars.com/kata/5f7c38eb54307c002a2b8cc8/solutions/c
  8.  
  9. I recommend the site www.codewars.com because it is great for learning programming languages.
  10. Its advantage over other similar sites is that once you solve the problem,
  11. you can see how other people have solved the same problem.
  12. And much can be learned from others ...
  13.  
  14. Remove the parentheses
  15.  
  16. In this kata you are given a string for example:
  17.  
  18. "example(unwanted thing)example"
  19. Your task is to remove everything inside the parentheses as well as the parentheses themselves.
  20.  
  21. The example above would return:
  22.  
  23. "exampleexample"
  24. Notes
  25. Other than parentheses only letters and spaces can occur in the string.
  26. Don't worry about other brackets like "[]" and "{}" as these will never appear.
  27. There can be multiple parentheses.
  28. The parentheses can be nested.
  29.  
  30.  
  31.     You can find all my C programs at Dragan Milicev's pastebin:
  32.  
  33.     https://pastebin.com/u/dmilicev
  34.  
  35. */
  36.  
  37. #include <stdio.h>
  38. #include <string.h>
  39.  
  40. // implementation of C function strcpy()
  41. void my_strcpy(const char *str_in, char *str_out) {
  42.     //while(*str_in != '\0'){
  43.     while(*str_in){
  44.         *str_out = *str_in;
  45.         str_in++;
  46.         str_out++;
  47.     }
  48.  
  49.     *str_out = '\0';    // include the terminating null character
  50. }
  51.  
  52. // implementation of C function strcpy()
  53. void my_strcpy1(const char *str_in, char *str_out) {
  54.     while(*str_in)
  55.         *str_out++ = *str_in++;
  56.     *str_out = '\0';    // include the terminating null character
  57. }
  58.  
  59. void count_parentheses(const char *str_in, char *str_out) {
  60.     int i=0, j=0, opened_parentheses=0, closed_parentheses=0, active_parentheses=0;
  61.     while( *(str_in+i) ){
  62.         if( *(str_in+i) == '(' ){
  63.             opened_parentheses++;
  64.             active_parentheses++;
  65.         }
  66.         if( *(str_in+i) == ')' ){
  67.             closed_parentheses++;
  68.             active_parentheses--;
  69.         }
  70.         printf("\n i = %3d  *(str_in+i) = %c  opened = %d   closed = %d  active = %d \n",
  71.                 i, *(str_in+i), opened_parentheses, closed_parentheses, active_parentheses );
  72.         if( active_parentheses == 0 && *(str_in+i) != ')' ){
  73.             *(str_out+j) = *(str_in+i); // copy
  74.             j++;
  75.         }
  76.         i++;
  77.     }
  78.     *(str_out+j) = '\0';                // include the terminating null character
  79. }
  80.  
  81. // do not allocate memory for return value
  82. // assign to the provided pointer *str_out
  83. void remove_parentheses(const char *str_in, char *str_out) {
  84.     int i=0, j=0, active_parentheses=0;
  85.     while( *(str_in+i) ){
  86.         if( *(str_in+i) == '(' )
  87.             active_parentheses++;
  88.         if( *(str_in+i) == ')' )
  89.             active_parentheses--;
  90.         if( active_parentheses == 0 && *(str_in+i) != ')' ){
  91.             *(str_out+j) = *(str_in+i); // copy
  92.             j++;
  93.         }
  94.         i++;
  95.     }
  96.     *(str_out+j) = '\0';                // include the terminating null character
  97. }
  98.  
  99. void remove_parentheses1(const char *str_in, char *str_out) {
  100.     int active_parentheses=0;
  101.     while( *str_in ){
  102.         if( *str_in == '(' )
  103.             active_parentheses++;
  104.         if( *str_in == ')' )
  105.             active_parentheses--;
  106.         if( active_parentheses == 0 && *str_in != ')' ){
  107.             *str_out = *str_in;     // copy
  108.             str_out++;
  109.         }
  110.         str_in++;
  111.     }
  112.     *str_out = '\0';                // include the terminating null character
  113. }
  114.  
  115. void remove_parentheses2(const char *str_in, char *str_out) {
  116.   int i, k, c = ' ', count = 0;
  117.   for(i=0, k=0; c!='\0'; i++){
  118.     c = str_in[i];
  119.     if( c == '(' ) count++;
  120.     if( !count ) str_out[k++] = c;
  121.     if(c == ')') count--;;
  122.   }
  123. }
  124.  
  125. void remove_parentheses3(const char *in, char *out){
  126.     int n;
  127.     for (n=0; *in; in++){
  128.         n += *in == '(';
  129.         if (!n)
  130.             *out++ = *in;
  131.         n -= *in == ')';
  132.     }
  133.     *out = 0;   // must be 0, not '\0' !
  134. }
  135.  
  136. int main(void){
  137.     int i, n;
  138.     char str_in[1024], str_out[1024];
  139.  
  140.     sprintf(str_in, "example(unwanted thing)example");
  141. //  sprintf(str_in, "example(unwanted thingexample)");
  142. //  sprintf(str_in, "exam(ple(unwanted th)ing)example");
  143. //  sprintf(str_in, "ex(amp)le(unwanted thing)example");
  144. //  sprintf(str_in, "ex(a(mp)le(unwanted thing)ex)ample");
  145. //  sprintf(str_in, "fgLiXvSh YTkLFJtSKCiLSEVlQybsQcxWNtQfiX((NbocKHIrhQPMf(XITL G)(AxoaCj)Gavv(lcJTh)tW)yoWUe()LoM)L");
  146. //  sprintf(str_in, "cRoxCdzqCPVTsOqEeLIspROzLzVFzHHBwJLYzh  JHF(fVDjqZCtAaFizxNLb(jkBHW PIMvmoueWLo(HDp)qU DbAlTsiaTP(DB(mKnnbsWzt)TWV)(N)zBKL) T)");
  147.  
  148. //  printf("\n str_in  = |%s| \n", str_in);
  149.  
  150. //  count_parentheses(str_in, str_out);
  151. //  remove_parentheses(str_in, str_out);
  152.     remove_parentheses1(str_in, str_out);
  153. //  remove_parentheses2(str_in, str_out);
  154. //  remove_parentheses3(str_in, str_out);
  155.  
  156. //  my_strcpy(str_in,str_out);
  157. //  my_strcpy1(str_in,str_out);
  158.  
  159.     printf("\n str_in  = |%s| \n", str_in);
  160.     printf("\n str_out = |%s| \n", str_out);
  161.  
  162.     printf("\n\n");
  163.     return 0;
  164. } // main()
  165.  
Advertisement
RAW Paste Data Copied
Advertisement