brie,

This gave me the idea to do the same in C, but use the argument string itself as the stack to avoid any allocations. It could probably be further optimized.


<span style="color:#323232;">#include <stdio.h>
</span><span style="color:#323232;">
</span><span style="color:#323232;">int main(int argc, char **argv)
</span><span style="color:#323232;">{
</span><span style="color:#323232;">	char map[256] = { 0 };
</span><span style="color:#323232;">	map[')'] = '(';
</span><span style="color:#323232;">	map['}'] = '{';
</span><span style="color:#323232;">	map[']'] = '[';
</span><span style="color:#323232;">
</span><span style="color:#323232;">	while (--argc) {
</span><span style="color:#323232;">		char *top, *p, c;
</span><span style="color:#323232;">		top = p = *++argv;
</span><span style="color:#323232;">
</span><span style="color:#323232;">		while ((c = *p++)) {
</span><span style="color:#323232;">			if (top != *argv && map[(size_t)c] == top[-1]) {
</span><span style="color:#323232;">				top--;
</span><span style="color:#323232;">			} else {
</span><span style="color:#323232;">				*top++ = c;
</span><span style="color:#323232;">			}
</span><span style="color:#323232;">		}
</span><span style="color:#323232;">
</span><span style="color:#323232;">		*top = 0;
</span><span style="color:#323232;">
</span><span style="color:#323232;">		puts(*argv);
</span><span style="color:#323232;">	}
</span><span style="color:#323232;">}
</span>
  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • aaaaaaacccccccce
  • test
  • CafeMeta
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • KamenRider
  • Ask_kbincafe
  • TheResearchGuardian
  • KbinCafe
  • Socialism
  • oklahoma
  • SuperSentai
  • feritale
  • All magazines