-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackWord.java
More file actions
41 lines (28 loc) · 1001 Bytes
/
Copy pathStackWord.java
File metadata and controls
41 lines (28 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;
import java.util.Stack;
public class StackWord {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println("enter a sentence: ");
String input = scan.nextLine();
String words[]= input.split(" ");
Stack<String> s = new Stack<String>();
for(int i = 0 ; i < words.length ; i++){
s.push(words[i]);
}
for(int i = 0 ; i<words.length ; i++){
System.out.println(s.pop()+" ");
}
Scanner scanning = new Scanner(System.in);
System.out.println("enter a word: ");
String input2 = scanning.nextLine();
char[] word = input2.toCharArray();
Stack<Character> stack = new Stack<Character>();
for(int i = 0 ; i < word.length ; i++){
stack.push(word[i]);
}
for(int i = 0 ; i< word.length ; i++){
System.out.print(stack.pop());
}
}
}