In: Computer Science
Write a Java application that checks a string for correct parenthesization. The input comes from the keyboard and should (but might not) consist solely of the delmiters (, ), [, ], {, and } separated by whitespace.
The output of the program must include following:
The name of your class should be ParenChecker.
For example, if the input was:
( [ { } ] )
the program should produce the following output:
OK
and if the input was:
(
the program should produce the output:
Missing closer
etc...
Code:
import java.util.Scanner;
class ParenChecker{
   public static void main(String args[]){
       Scanner s=new
Scanner(System.in);
       System.out.print("enter
input:");       //reading
input.
       String
k=s.next();   
       int top=-1,i,c=0;
       char j;
       char a[]=new char[100];
       for(i=0;i<k.length();i++){
          
j=k.charAt(i);
           if(j=='(' ||
j=='{' || j=='['){
          
   
a[++top]=j;                  
//push into the stack.
          
           }
           else if(top!=-1
&& (j=='}' || j==']' || j==')' ) ){
          
    if(j==']' && a[top]=='[')
          
        top=top-1;
          
    else if(j=='}' && a[top]=='{')
          
        top=top-1;
          
    else if(j==')' && a[top]=='(')
          
        top=top-1;
          
    else{
          
       
System.out.println("Opener/Closer mismatch");
          
        c=1;
          
        break;
          
    }
           }
           else{
          
    System.out.println("Unexpected
symbol");        //if unexpected
symbol occurs other than brackets.
          
    break;
          
           }
       }
       if(top==-1)
          
System.out.println("OK");      
//printing ok if parenthesis is correct.
       else if(top!=-1 &&
c!=1){
          
j=k.charAt(0);
           if(j=='(' ||
j=='{' || j=='[')
          
    System.out.println("Missing
Closer");     
           if(j==')' ||
j=='}' || j==']')
          
    System.out.println("Missing Opener");
       }   
   }
}
Output 1:

Output 2:
