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

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









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
Write a method which will remove any given character from a string?
How to match a string using the INDEX and the method use Stack or Queue?
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.
write a java program for inserting a substring in to the given main string from a given position
how to write a function to print for finding the longest palindrome in the given string
find the given input is integer or string
Help me on the given problem of String
How to display string or character
iPhone String Match
Arraylist java code - Java Interview Questions
Day for the given Date in Java
Evaluating a math expression given in string form
Finding the longest word in a user given string
java code for given query
how to find the given date is sunday
Java Remove a character from string
How to parse data in JSON format
program on factorial of a given number in java
program on factorial of a given number in java
Java: Some problems in given condition
String Start with Example
How to get given index value from FloatBuffer in java.
Writes the given double value into a buffer at the given index.
Java generate the powerset of a given set
charAt() method in java
find all unique character in string using java
How to index a given paragraph in alphabetical order
Please provide the java code for the given program.
php parse string into array
How to match a string symbols using the INDEX and the method use Stack
How to write the given byte into byte buffer.
Parsing Character-Separated Data with a Regular Expression
Java converts any given year to Roman Numeral
how to parse json in python and get data
parse data from a link in java
coding for given question
Retrieving String after given Regular expression
String Determining a Character's Unicode Block
socket connection: how to parse data from server?
php parse string to time
how to match the key word from a text file
php parse string for url
how to split string after first occurrence of alphanumeric character?
Convert Character into a String
How to Display Data in a tree structure on the GUI

Ads