Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Created October 30, 2012 07:44
Show Gist options
  • Select an option

  • Save neuro-sys/3978836 to your computer and use it in GitHub Desktop.

Select an option

Save neuro-sys/3978836 to your computer and use it in GitHub Desktop.
Paranthesis matching in text
import java.util.Stack;
public class TestFirat {
private static String s = "()(((()))";
public static void main(String[] args) {
Stack<Character> stack = new Stack<Character>();
boolean error = false;
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
if (c == '(')
stack.push(s.charAt(i));
else if (c == ')')
if ((error = stack.isEmpty() || stack.pop() != '(')) {
System.out.println("Character at " + i + " is not matching:");
System.out.println(s);
while (i-- != 0) System.out.print(' '); System.out.println('^');
break;
}
}
if (!error && !stack.isEmpty()) { /* TODO: Find which left brace needs a closing match. */
System.out.println("No closing match found.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment