Created
October 30, 2012 07:44
-
-
Save neuro-sys/3978836 to your computer and use it in GitHub Desktop.
Paranthesis matching in text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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