In data structure in java how to parse the given string and counts the member of character that match the given data?

Ads
 

In data structure in java how to parse the given string and counts the member of character that match the given data?

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

Ads









Related Tutorials/Questions & Answers:
In data structure in java how to parse the given string and counts the member of character that match the given data?
Writes the given character into buffer at the given index.
Advertisements
How to match a string using the INDEX and the method use Stack or Queue?
Write a method which will remove any given character from a string?
Replace Character in String
plz give me program to this: Given a string and a number ‘n’, find the ‘n’th distinct repeating character.
2. Given a string and a number ‘n’, find the ‘n’th distinct repeating character.
Compare String with given String arraylists.
how to write a function to print for finding the longest palindrome in the given string
write a java program for inserting a substring in to the given main string from a given position
How to display string or character
find the given input is integer or string
Help me on the given problem of String
Arraylist java code - Java Interview Questions
Day for the given Date in Java
iPhone String Match
how to find the given date is sunday
java code for given query
Evaluating a math expression given in string form
Finding the longest word in a user given string
Java Remove a character from string
program on factorial of a given number in java
program on factorial of a given number in java
How to get given index value from FloatBuffer in java.
Java: Some problems in given condition
Java generate the powerset of a given set
How to Display Data in a tree structure on the GUI
String Start with Example
How to index a given paragraph in alphabetical order
parse data from a link in java
How to match a string symbols using the INDEX and the method use Stack

Ads