Hi
this is my project question in Data Structures
and this is the code
PHP Code:
import java.util.Scanner;
class Stack {
private int top = -1;
private char stack[] = new char[10];
public void push(char a) {
stack[++top] = a;
}
public char pop() {
return (char) stack[top--];
}
public void refill() {
top = stack.length - 1;
}
public void reset() {
top = -1;
}
}
public class Class1 {
public static void main(String[] args) throws Exception {
Stack s1 = new Stack();
Stack s2 = new Stack();
Stack s3 = new Stack();
boolean isPartOf = false;
Scanner input = new Scanner(System.in);
int n, r = 0;
char ch, ch1, ch2;
System.out.println("========================================================================= ");
System.out.println("Enter 10 letters for the language , , , WITHOUT SPACES BETWEEN THE LETTERS");
for (int i = 0; i < 10; i++) {
ch = (char) System.in.read();
s1.push(ch);
}
System.out.println("========================================================================= ");
System.out.println("The valid characters of your language are:");
for (int i = 0; i < 10; i++) {
System.out.print(i + 1);
System.out.println(") ");
}
System.out.println("========================================================================= ");
System.out.println("Enter the number of the letters of the word you want to check");
n = input.nextInt();
System.out.println("========================================================================= ");
System.out.println("Enter the word you need to check");
for (int i = 0; i < n; i++) {
ch = (char) System.in.read();
s2.push(ch);
}
for (int i = 0; i < n; i++) {
ch1 = s2.pop();
s1.refill();
for (int j = 0; j < 10; j++) {
ch2 = s1.pop();
if (ch1 == ch2) {
isPartOf = true;
r++;
s3.push(ch2);
System.out.println("ch2 = " + ch2);
} else if (isPartOf) {
isPartOf = false;
break;
}
}
}
if (isPartOf) {
System.out.println("========================================================================= ");
System.out.println("The word is part of the language\n");
} else {
System.out.println("========================================================================= ");
System.out.println("The word is not a part of the langauge\n");
}
}
}
it is working but not in all cases
where is the wrong