Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solver
- {
- public static void main(String args[])
- {
- System.out.println(reverse("DR Sahar 3yota"));
- }
- public static StringBuilder reverse(String inp)
- {
- StringBuilder ret = new StringBuilder("");
- Stack st = new Stack();
- for (int i = 0; i < inp.length(); i++)
- {
- char c = inp.charAt(i);
- if (c != ' ')st.push(c);
- else
- {
- while (st.isEmpty() == false)
- {
- ret.append(st.pop().ch);
- }
- ret.append(' ');
- }
- }
- while (st.isEmpty() == false)
- {
- ret.append(st.pop().ch);
- }
- return ret;
- }
- }
- class Node
- {
- char ch;
- Node next;
- public Node(char c)
- {
- ch = c;
- }
- }
- class Stack
- {
- Node Top;
- public void push(char c)
- {
- Node temp = new Node(c);
- temp.next = Top;
- Top = temp;
- return;
- }
- public Node pop()
- {
- if (isEmpty())
- {
- System.out.println("Can't pop, The Stack is Empty");
- return null;
- }
- Node temp = Top;
- Top = Top.next;
- return temp;
- }
- public Node top()
- {
- if (isEmpty())
- {
- System.out.println("The Stack is Empty");
- }
- return Top;
- }
- public boolean isEmpty()
- {
- return Top == null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment