OK, turing.

<- leave blank

Sun Feb 10 15:41:54 EST 2019

#include <u.h>
#include <libc.h>
#include <stdio.h>

char* readnflip(char *in) {
	char c;
	char *buf = calloc(1, sizeof(char));
	char *p;
	char *q;
	static char *st;
	int l, i;
	int L = 1;

	for (q = in; *q != '\0'; q++) {
		c = *q;
		switch (c) {
		case '(':
			p = readnflip(q+1);
			q = st;
			l = strlen(p);
			buf = realloc(buf, l + L);
			for (i = 0; i < l; i++) {
				buf[L+i-1] = p[l-i-1];
			}
			L += l;
			free(p);
			buf[L-1] = '\0';
			break;
		case ')':
		case '\n':
			st = q;
			return buf;
		default:
			L++;
			buf = realloc(buf, L);
			buf[L-1] = '\0';
			buf[L-2] = c;
			break;
		}
	}
	return buf;
}

void main() {
	char *t;
	char *s = malloc(1024);

	while(fgets(s, 1023, stdin) != nil) {
		t = readnflip(s);
		printf("%s\n", t);
		free(t);
	}
}