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 – 14 years ago
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 – 14 years ago@Joey, I'd assume not, though that would be all the more impressive. – Neil – 14 years ago
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 – 14 years ago
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 – 14 years ago
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 – 14 years ago
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 – 14 years ago
Are leading/trailing newlines allowed? – Lowjacker – 14 years ago
Lowjacker assume that no newlines in the input – Prashant Bhate – 14 years ago
1@Phrasant Bhate: And in the output? – Lowjacker – 14 years ago