contoh output : pagi indah -> hadni igap
berikut source codenya :
import java.io.*;
class stackchar{
private int ukuran;
private char[] tumpukan;
private int top;
public stackchar(int s){
ukuran = s;
tumpukan = new char[ukuran];
top = -1;
}
public void push(char j){
tumpukan[++top] = j;
}
public char pop(){
return tumpukan[top--];
}
public char peek(){
return tumpukan[top];
}
public boolean isEmpty(){
return(top == -1);
}
public boolean isFull(){
return(top == ukuran -1);
}
public void baca(){
int i = top;
while(i<=0){
System.out.print(tumpukan[i]);
System.out.print("");
i--;
}
System.out.println();
}
}
public class stack{
public static void main(String []args)throws IOException{
String kalimat, output;
int i;
stackchar jmlhuruf = new stackchar(20);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("masukkan kata : ");
kalimat = in.readLine();
for (i=0;i<kalimat.length();i++){
jmlhuruf.push(kalimat.charAt(i));
}
output = "";
while (!jmlhuruf.isEmpty()){
output = output+jmlhuruf.pop();
}
System.out.println("dibalik menjadi = "+output);
}
}
0 komentar:
Posting Komentar