Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- remove_the_parentheses.c
- https://www.codewars.com/kata/5f7c38eb54307c002a2b8cc8
- https://www.codewars.com/kata/5f7c38eb54307c002a2b8cc8/train/c
- https://www.codewars.com/kata/5f7c38eb54307c002a2b8cc8/solutions/c
- I recommend the site www.codewars.com because it is great for learning programming languages.
- Its advantage over other similar sites is that once you solve the problem,
- you can see how other people have solved the same problem.
- And much can be learned from others ...
- Remove the parentheses
- In this kata you are given a string for example:
- "example(unwanted thing)example"
- Your task is to remove everything inside the parentheses as well as the parentheses themselves.
- The example above would return:
- "exampleexample"
- Notes
- Other than parentheses only letters and spaces can occur in the string.
- Don't worry about other brackets like "[]" and "{}" as these will never appear.
- There can be multiple parentheses.
- The parentheses can be nested.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <string.h>
- // implementation of C function strcpy()
- void my_strcpy(const char *str_in, char *str_out) {
- //while(*str_in != '\0'){
- while(*str_in){
- *str_out = *str_in;
- str_in++;
- str_out++;
- }
- *str_out = '\0'; // include the terminating null character
- }
- // implementation of C function strcpy()
- void my_strcpy1(const char *str_in, char *str_out) {
- while(*str_in)
- *str_out++ = *str_in++;
- *str_out = '\0'; // include the terminating null character
- }
- void count_parentheses(const char *str_in, char *str_out) {
- int i=0, j=0, opened_parentheses=0, closed_parentheses=0, active_parentheses=0;
- while( *(str_in+i) ){
- if( *(str_in+i) == '(' ){
- opened_parentheses++;
- active_parentheses++;
- }
- if( *(str_in+i) == ')' ){
- closed_parentheses++;
- active_parentheses--;
- }
- printf("\n i = %3d *(str_in+i) = %c opened = %d closed = %d active = %d \n",
- i, *(str_in+i), opened_parentheses, closed_parentheses, active_parentheses );
- if( active_parentheses == 0 && *(str_in+i) != ')' ){
- *(str_out+j) = *(str_in+i); // copy
- j++;
- }
- i++;
- }
- *(str_out+j) = '\0'; // include the terminating null character
- }
- // do not allocate memory for return value
- // assign to the provided pointer *str_out
- void remove_parentheses(const char *str_in, char *str_out) {
- int i=0, j=0, active_parentheses=0;
- while( *(str_in+i) ){
- if( *(str_in+i) == '(' )
- active_parentheses++;
- if( *(str_in+i) == ')' )
- active_parentheses--;
- if( active_parentheses == 0 && *(str_in+i) != ')' ){
- *(str_out+j) = *(str_in+i); // copy
- j++;
- }
- i++;
- }
- *(str_out+j) = '\0'; // include the terminating null character
- }
- void remove_parentheses1(const char *str_in, char *str_out) {
- int active_parentheses=0;
- while( *str_in ){
- if( *str_in == '(' )
- active_parentheses++;
- if( *str_in == ')' )
- active_parentheses--;
- if( active_parentheses == 0 && *str_in != ')' ){
- *str_out = *str_in; // copy
- str_out++;
- }
- str_in++;
- }
- *str_out = '\0'; // include the terminating null character
- }
- void remove_parentheses2(const char *str_in, char *str_out) {
- int i, k, c = ' ', count = 0;
- for(i=0, k=0; c!='\0'; i++){
- c = str_in[i];
- if( c == '(' ) count++;
- if( !count ) str_out[k++] = c;
- if(c == ')') count--;;
- }
- }
- void remove_parentheses3(const char *in, char *out){
- int n;
- for (n=0; *in; in++){
- n += *in == '(';
- if (!n)
- *out++ = *in;
- n -= *in == ')';
- }
- *out = 0; // must be 0, not '\0' !
- }
- int main(void){
- int i, n;
- char str_in[1024], str_out[1024];
- sprintf(str_in, "example(unwanted thing)example");
- // sprintf(str_in, "example(unwanted thingexample)");
- // sprintf(str_in, "exam(ple(unwanted th)ing)example");
- // sprintf(str_in, "ex(amp)le(unwanted thing)example");
- // sprintf(str_in, "ex(a(mp)le(unwanted thing)ex)ample");
- // sprintf(str_in, "fgLiXvSh YTkLFJtSKCiLSEVlQybsQcxWNtQfiX((NbocKHIrhQPMf(XITL G)(AxoaCj)Gavv(lcJTh)tW)yoWUe()LoM)L");
- // sprintf(str_in, "cRoxCdzqCPVTsOqEeLIspROzLzVFzHHBwJLYzh JHF(fVDjqZCtAaFizxNLb(jkBHW PIMvmoueWLo(HDp)qU DbAlTsiaTP(DB(mKnnbsWzt)TWV)(N)zBKL) T)");
- // printf("\n str_in = |%s| \n", str_in);
- // count_parentheses(str_in, str_out);
- // remove_parentheses(str_in, str_out);
- remove_parentheses1(str_in, str_out);
- // remove_parentheses2(str_in, str_out);
- // remove_parentheses3(str_in, str_out);
- // my_strcpy(str_in,str_out);
- // my_strcpy1(str_in,str_out);
- printf("\n str_in = |%s| \n", str_in);
- printf("\n str_out = |%s| \n", str_out);
- printf("\n\n");
- return 0;
- } // main()
Advertisement
RAW Paste Data
Copied
Advertisement