Hi im a newbie for java can u help me this kind of problem.the problem goes like this,The problem that is given to our teacher is like this design a parser that parse the given string and counts the number of characters that match the given data .Using stack and queue the output should be like this
given string: A B C D E F G
input string : a c c d e f c
no.of matches : 5
8 replies to this topic
#1
Posted 10 November 2010 - 03:55 AM
|
|
|
#2
Posted 10 November 2010 - 04:10 AM
you have a list or array of given Strings g
you have a String containing the input string called s
the code could look a little bit like this (temp is a list of String):
you have a String containing the input string called s
the code could look a little bit like this (temp is a list of String):
for every element x of s if(g.contains(x) && !temp.contains(x)) temp.add(x); return temp.size();
#3
Posted 10 November 2010 - 04:41 AM
I'm not sure whether the string must contain each letter, or the position must be the same. In your example it's 5 for both ways ^^
#4
Posted 10 November 2010 - 05:21 AM
oxano said:
I'm not sure whether the string must contain each letter, or the position must be the same. In your example it's 5 for both ways ^^
Right, I didn't notice that. Of course my example works for the first hypothesis.
#5
Posted 11 November 2010 - 03:34 PM
import java.util.Stack;
public class machineproblem1
{
public static void main(String [] args){
Stack s = new Stack ();
s.push("A");s.push("B");s.push("C");
s.push("A");s.push("t");s.push("C");
System.out.println(s);
if(s.peek()=="t");
{
System.out.println(" "+ s.size());
}
if(s.size()==0){
System.out.println(" stack is empty");
}else{
System.out.println("Stack Contains" + s.size() + " items");
}
while (!s.empty()){
System.out.println(s.pop());
}
}
}
i try to solve it last night but the problem is ..is how to compare the input character to the exsisting character..I need your help guys to come up with e exact and right logic or flow of program..coz im a newbie for this thing
Edited by Alexander, 11 November 2010 - 03:47 PM.
(bbcode tags)
#6
Posted 11 November 2010 - 03:39 PM
Please explain exactly what your program is supposed to do
#7
Posted 12 November 2010 - 03:00 AM
import java.util.*;
public class StackDemo{
public static void main(String[] args) {
Stack stack=new Stack();
stack.push(new String("a"));
stack.push("a");
stack.push("b");
System.out.print("Given String");
System.out.println(stack);
i don't know the next procedure on how to compare the input string to the given string..can u help me how?
input string : a c c d e f c
no.of matches : 5
public class StackDemo{
public static void main(String[] args) {
Stack stack=new Stack();
stack.push(new String("a"));
stack.push("a");
stack.push("b");
System.out.print("Given String");
System.out.println(stack);
i don't know the next procedure on how to compare the input string to the given string..can u help me how?
input string : a c c d e f c
no.of matches : 5
#8
Posted 17 November 2010 - 05:27 AM
import java.util.*;
class StackParse{
public static void main(String[] args){
Scanner console=new Scanner(System.in);
String given, inputstr;
String indexes="";
int cntfind=0;
System.out.println("Given/Input a string:");
given=console.nextLine();
System.out.println("Input a string:");
inputstr=console.nextLine();
Stack stack=new Stack();
//stack.push(new String());
stack.push("A");
stack.push("B");
stack.push("C");
stack.push("D");
stack.push("E");
stack.push("F");
stack.push("G");
System.out.println((stack));
//parse here using string(array of character)
for(int i=0;i< given.length(); i++){
for(int m=0;m< inputstr.length(); m++){
if(inputstr.charAt(m) == given.charAt(i)){
cntfind++;
indexes+=i+",";
}
}
}
System.out.println("Input String :"+inputstr);
System.out.println("NO. OF MATCHES :"+cntfind);
System.out.println("Indexes OF MATCHES :"+indexes);
}
}
I have already the solution but the problem is..how to implement this using stack...
class StackParse{
public static void main(String[] args){
Scanner console=new Scanner(System.in);
String given, inputstr;
String indexes="";
int cntfind=0;
System.out.println("Given/Input a string:");
given=console.nextLine();
System.out.println("Input a string:");
inputstr=console.nextLine();
Stack stack=new Stack();
//stack.push(new String());
stack.push("A");
stack.push("B");
stack.push("C");
stack.push("D");
stack.push("E");
stack.push("F");
stack.push("G");
System.out.println((stack));
//parse here using string(array of character)
for(int i=0;i< given.length(); i++){
for(int m=0;m< inputstr.length(); m++){
if(inputstr.charAt(m) == given.charAt(i)){
cntfind++;
indexes+=i+",";
}
}
}
System.out.println("Input String :"+inputstr);
System.out.println("NO. OF MATCHES :"+cntfind);
System.out.println("Indexes OF MATCHES :"+indexes);
}
}
I have already the solution but the problem is..how to implement this using stack...
#9
Posted 23 November 2010 - 05:29 PM
import java.util.*;
class StackParse{
public static void main(String[] args){
Scanner console=new Scanner(System.in);
String given, inputstr;
String indexes="";
int cntfind=0;
System.out.println("Given/Input a string:");
given=console.nextLine();
System.out.println("Input a string:");
inputstr=console.nextLine();
Stack stack=new Stack();
//stack.push(new String());
stack.push("A");
stack.push("B");
stack.push("C");
stack.push("D");
stack.push("E");
stack.push("F");
stack.push("G");
System.out.println((stack));
//parse here using string(array of character)
for(int i=0;i< given.length(); i++){
for(String m:inputstr){
for (int n:stack){
if(stack.equals(inputstr.charAt(m))){
cntfind++;
indexes+=i+",";
}
}
}
}
System.out.println("Input String :"+inputstr);
System.out.println("NO. OF MATCHES :"+cntfind);
System.out.println("Indexes OF MATCHES :"+indexes);
}
}
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









