Balanced brackets: Difference between revisions

(added C code)
Line 418:
bad: [[]][[]]]]][[[
bad: ][]]][[[[][][][]</pre>
 
"Manual" balance check:
 
<lang d>import std.stdio, std.array, std.algorithm, std.string, std.random;
 
int main(string[] argv)
{
NEXT_STR:
for(int j=0; j < 10; j++) {
auto s = "[]".repeat(3).dup;
randomShuffle(s);
write(`String: `~ s);
int balance;
foreach (int i, char c; s) {
balance += (c == '[' ? 1 : (c == ']' ? -1 : 0));
if (balance < 0) { writeln(` BAD`); continue NEXT_STR; }
}
writeln(` OK!`);// for equal amount of open/closed brackets, balance here is always = 0
}
return 0;
}</lang>
 
Output:
<pre>
String: ]]][[[ BAD
String: ][[[]] BAD
String: ]][][[ BAD
String: [[[]]] OK!
String: ]][[[] BAD
String: ][[[]] BAD
String: [][[]] OK!
String: ][][[] BAD
String: [[]]][ BAD
String: []][][ BAD
</pre>
 
=={{header|Euphoria}}==
19

edits