An interview question I read:
Let's say you're on my team, and I've decided I'm a real stickler for code formatting. But I've got peculiar tastes, and one day I decide I want to have all parentheses stand out very clearly in your code.
So let's say you've got a set of source files in C, C++, or Java. Your choice. And I want you to modify them so that in each source file, every open- and close-parenthesis has exactly one space character before and after it. If there is any other whitespace around the parenthesis, it's collapsed into a single space character.
For instance, this code:
foo (bar ( new Point(x, graph.getY()) ));
Would be modified to look like this:
foo ( bar ( new Point ( x, graph.getY ( ) ) ) );
How to do this with sed ?
EDIT
my approach
sed -Ee 's/([()])([()])([()])/\1 \2 \3 /g' -e 's/([ ]{1,}|^|([0-9a-zA-Z()]))([()])([ ]{1,}|$|([0-9;a-z()]))/\2 \3 \5/g' filename.c
This fails at cases like func(a);
my code outputs it as func ( a);