Mathematica (non code golf)
indent[str_String]:=Module[{ind,indent,f},
ind=0;
indent[i_]:="\n"<>Nest[" "<>ToString[#]&,"",i];
f[c_] := (indent[ind] <> c <> indent[++ind]) /; StringMatchQ["[({",___~~c~~___];
f[c_] := ( indent[--ind] <> c <>indent[ind]) /; StringMatchQ["])}",___~~c~~___];
f[c_] := (c <>indent[ind]) /; StringMatchQ[";,",___~~c~~___];
f[c_] := c ;
f /@ Characters@ str//StringJoin
]
Test
indent["abc{xyz{text[note{comment(t{ex}t)abc}]}}"]
abc
{
xyz
{
text
[
note
{
comment
(
t
{
ex
}
t
)
abc
}
]
}
}
As a bonus, following function can be used to format mathematica expression
format[expr_] := indent[expr // FullForm // ToString]
EDIT(non code golf)
Updated with fine granular control over the way newlines are rendered
indent[str_String, ob_String, cb_String, delim_String] :=
Module[{ind, indent, f, tab}, ind = 0; tab = " ";
indent[i_, tab_, nl_] := nl <> Nest[tab <> ToString[#] &, "", i];
f[c_] := (indent[ind, "", " "] <> c <> indent[++ind, tab, "\n"]) /;StringMatchQ[ob, ___ ~~ c ~~ ___];
f[c_] := (indent[--ind, "", " "] <> c <> indent[ind, tab, "\n"]) /;StringMatchQ[cb, ___ ~~ c ~~ ___];
f[c_] := (c <> indent[ind, tab, "\n"]) /;StringMatchQ[delim, ___ ~~ c ~~ ___];
f[c_] := c;
f /@ Characters@str // StringJoin];
format[expr_] := indent[expr // InputForm // ToString, "[({", "])}", ";"];
format[Hold@Module[{ind, indent, f, tab}, ind = 0; tab = " ";
indent[i_, tab_, nl_] := nl <> Nest[tab <> ToString[#] &, "", i];
f[c_] := (indent[ind, "", " "] <> c <> indent[++ind, tab, "\n"]) /;StringMatchQ[ob, ___ ~~ c ~~ ___];
f[c_] := (indent[--ind, "", " "] <> c <> indent[ind, tab, "\n"]) /;StringMatchQ[cb, ___ ~~ c ~~ ___];
f[c_] := (c <> indent[ind, tab, "\n"]) /;StringMatchQ[delim, ___ ~~ c ~~ ___];
f[c_] := c;
f /@ Characters@str // StringJoin]]
Output
Hold [
Module [
{
ind, indent, f, tab }
, ind = 0;
tab = " ";
indent [
i_, tab_, nl_ ]
:= StringJoin [
nl, Nest [
StringJoin [
tab, ToString [
#1 ]
]
& , "", i ]
]
;
f [
c_ ]
:= StringJoin [
indent [
ind, "", " " ]
, c, indent [
++ind, tab, "\n" ]
]
/;
StringMatchQ [
ob, ___~~c~~___ ]
;
f [
c_ ]
:= StringJoin [
indent [
--ind, "", " " ]
, c, indent [
ind, tab, "\n" ]
]
/;
StringMatchQ [
cb, ___~~c~~___ ]
;
f [
c_ ]
:= StringJoin [
c, indent [
ind, tab, "\n" ]
]
/;
StringMatchQ [
delim, ___~~c~~___ ]
;
f [
c_ ]
:= c;
StringJoin [
f / @
Characters [
str ]
]
]
]
5Can we assume that for each parenthesis there's a closing one, and in the same order? – Juan – 2011-07-05T01:22:42.150
Does the program have to support any parenthesis characters given as arguments? e.g.
./program 'p' 'q' <<< '1p23p45q67q8'
Or does it need only support{[(<
and}])>
? – Joey Adams – 2011-07-05T03:27:29.200@Joey, I'd assume not, though that would be all the more impressive. – Neil – 2011-07-05T07:39:45.047
joey : input are 1. open parenthesis characters 2.close parenthesis chars 3. string to indent. Juan : we can assume that, though code need not rely on that, what I mean is if delim is part of opening parenthesis chars, increase indent, else if part of closing parenthesis chars decrease indent. – Prashant Bhate – 2011-07-05T11:25:55.983
I rewrote the task specification. Could you look over it and figure out whether that's roughly what you meant? I've tried to be clearer on the exact specifications as the previous wording gave rise to four wrong answers. – Joey – 2011-07-05T12:04:48.810
Another thing: Is this supposed to be a Code Golf? Your solution doesn't seem to imply this, but as a challenge it's fairly trivial. Please clarify. – Joey – 2011-07-05T15:12:01.473
considering forum yes you are right , changed answer to reflect the same. I was trying it in java and so posted it here ! – Prashant Bhate – 2011-07-05T15:44:29.800
Are leading/trailing newlines allowed? – Lowjacker – 2011-07-06T21:58:14.110
Lowjacker assume that no newlines in the input – Prashant Bhate – 2011-07-07T08:26:26.027
1@Phrasant Bhate: And in the output? – Lowjacker – 2011-07-07T11:37:34.763