How to match a string using the INDEX and the method use Stack or Queue?

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

THE MATCH OF THE STRING IS BASED ON THERE INDEX POSITION

 **Example output:**

    Enter a STRING: w o r l d
    Enter a STRING to be match: w o l d
    No.of matches:  two
    would you like to check other string? yes or no?
    yes

    Enter a STRING: h e l p
    Enter a STRING to be match: h e l
    No.of matches:  three
    would you like to check other string? yes or no?
    yes

    Enter a STRING: 1 2 3 4 5
    Enter a STRING to be match: 1 2 3 4 5
    No.of matches: five
    would you like to check other string? yes or no?
    yes

    Enter a STRING: h e l l o
    Enter a STRING to be match: e h o o
    No.of matches: zero
    would you like to check other string? yes or no?
    yes
View Answers

November 19, 2010 at 5:50 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.get(i)==stack2.get(i)){
                count++;
            }
        }
        System.out.println("No of matches: "+count);
    }
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter string: ");
        String st1=input.next();
        System.out.print("Enter string to match: ");
        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();
        System.out.println();
        System.out.print("Enter String: ");
        String str=input.next();
        if(con.equals("yes")){
        System.out.print("Enter string to match: ");
        String st=input.next();
            accept(str,st);
        }
        else{
            System.exit(0);
        }
        }
        while(!con.equals("No"));
        }
}

Thanks









Related Tutorials/Questions & Answers:
Advertisements