Home Answers Viewqa Java-Beginners In data structure in java how to parse the given string and counts the member of character that match the given data?

 
 


jandi
In data structure in java how to parse the given string and counts the member of character that match the given data?
1 Answer(s)      2 years and 6 months ago
Posted in : Java Beginners

Design a parser that parse the given string and counts the member of character that match the given data.using the STACK or QUEUE

Example output

**

GIVEN STRING: a b c d e f g
INPUT STRING: a c d e f
No.of matches:  five 
would you like to check other string? yes or no?

**

View Answers

November 12, 2010 at 12:57 PM


Hello Friend,

Try the following code:

import java.util.*;

class MatchString{
    public static void accept(String st1,String st2){
        int count=0;
        char ch1[]=st1.toCharArray();
        Stack<Character> stack1=new Stack<Character>();
        Stack<Character> stack2=new Stack<Character>();
        char ch2[]=st2.toCharArray();
        for(int i=0;i<ch1.length;i++){
            stack1.push(Character.valueOf(ch1[i]));
        }
        for(int i=0;i<ch2.length;i++){
            stack2.push(Character.valueOf(ch2[i]));
        }
        for(int i=0;i<stack2.size();i++){
            if(stack1.contains(stack2.get(i))){
                count++;
            }
        }
        System.out.println("No of matches: "+count);
    }
    public static void main(String[] args){
        String st1="abcdefg";
        Scanner input=new Scanner(System.in);
        System.out.println("Enter string: ");
        String st2=input.next();
        String con="";
        accept(st1,st2);
        do{
        System.out.println("Would you like to check other string? yes or no?");
        con=input.next();
        if(con.equals("yes")){
        System.out.println("Enter string: ");
        String st=input.next();
            accept(st1,st);
        }
        else{
            System.exit(0);
        }
        }
        while(!con.equals("No"));
        }
}

Thanks









Related Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.