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

Ads
 

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

Ads









Related Tutorials/Questions & Answers:
How to match a string symbols using the INDEX and the method use Stack
How to match a string using the INDEX and the method use Stack or Queue?
Advertisements
iPhone String Match
Java String using STACK - Java Beginners
Reverse String using Stack
How to use charAt() method in Java?
how can create album in java by using Stack ....
In data structure in java how to parse the given string and counts the member of character that match the given data?
Java get Stack Trace as String
how insert multiple images in mysql database with use of struts 1.3.8 or java method, with single bean,or using array
Match string using regular expression without Case sensitivity
String length without using length() method in java
stack using linked list
how to use String tokenizer on table that is retrieved from database using jsp servlet
array, index, string
Java Stack Example
Finding start and end index of string using Regular expression
JavaScript regex exec() method for text match.
match string to regular expression - Objective C
how to use toString method? - Java Beginners
how to use string of one class into another Class
how to use sendRedirect in doGet method in jsp?
How to use find method in Java - Java Beginners
Why we should use string args[] in main method in java?
String replaceFirst() Method
How to split string in Java using comma?
java using Stack - Java Beginners
Interchanging String using RegularExpression
java using Stack - Java Beginners
JDOM CDATA Example, Use of append(String str) method of CDATA class.
String indexOf method example

Ads