[ create a new paste ] login | about

Link: http://codepad.org/OmB5L1pV    [ raw code | output | fork ]

C++, pasted on Nov 21:
#include <iostream>

char* str_drep(char* s, char fc, char lc, char c){
	int   n;
	char* p, *i, *t = s;

	while(*s && (*s != fc))
		++s;

	for(p = s; *s; *s = *p){
		if(*p == fc){
			n = 1;
			i = p + 1;
			while(*i){
				if(*i == fc)
					++n;
				else if(*i == lc){
					if(--n == 0)
						break;
				}
				++i;
			}
			
			if(! *i || n > 0){
				while((*s = *p) != 0){
					++s;
					++p;
				}
				break;
			} else if((i - p) > 1){
				*s++ = *p++;
				*s++ = c;
				p    = i;
				continue;
			}
		}
		++s;
		++p;
	}
	return t;
}

int main(void){
	char s[] = "()(lisp)(((prolog)))(-(!(#(COBOL)).)-)(D)";

	std::cout << s << std::endl;
	std::cout << str_drep(s, '(', ')', ' ') << std::endl;
	return 0;
}


Output:
1
2
()(lisp)(((prolog)))(-(!(#(COBOL)).)-)(D)
()( )( )( )( )


Create a new paste based on this one


Comments: