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

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:
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
Java String using STACK - Java Beginners
iPhone String Match
How to convert a stack trace to a string?
How to convert a stack trace to a string?
Reverse String using Stack
String length without using length() method in java
how can create album in java by using Stack ....
Match string using regular expression without Case sensitivity
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
stack using linked list
Finding start and end index of string using Regular expression
Interchanging String using RegularExpression
ModuleNotFoundError: No module named 'queuey'
array, index, string
JavaScript regex exec() method for text match.
match string to regular expression - Objective C
charAt() method in java
java using Stack - Java Beginners
String indexOf method example
Java Stack Example
String fromCharCode() method example in Action Script3
java using Stack - Java Beginners
How to split string in Java using comma?
ModuleNotFoundError: No module named 'queuey-py'
How to use charAt() method in Java?
how to send spaces using get method.
how to count words in string using java
String replaceFirst() Method
Pointcut NameMethodMatch using BeanFactory
Stack
How to clean a buffer using clear method in java.
Using get method in preferences
Count words in a string method?
Java Stack Example
String indexOf() Method
how insert multiple images in mysql database with use of struts 1.3.8 or java method, with single bean,or using array
String indexOf() method in Java
Why we should use string args[] in main method in java?
ModuleNotFoundError: No module named 'queued'
how to use String tokenizer on table that is retrieved from database using jsp servlet
How to Print a Stack Trace Message
how to convert xml string to soap request object using axis1.4?
JDOM CDATA Example, Use of append(String str) method of CDATA class.
Java toUpperCase() Method
How you can escape a String for HTML using the StringEscapeUtils
Java collection Stack example
Search string using substring

Ads