JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Reverse a word or string using Stack data structure.


This example uses stack operations to reverse a word (string). First we push each character to the stack, then we will pop each char from the stack.


package com.java2novice.ds.stack;

public class MyWordReverse {

	public String reverseWord(String word){
		
		StringBuilder sb = new StringBuilder();
		int size = word.length();
		StackImpl stack = new StackImpl(size);
		for(int i=0;i<size;i++){
			stack.push(word.charAt(i));
		}
		while(!stack.isStackEmpty()){
			sb.append(stack.pop());
		}
		return sb.toString();
	}
	
	public static void main(String a[]){
		MyWordReverse mwr = new MyWordReverse();
		System.out.println("Java2Novice == "+mwr.reverseWord("Java2Novice"));
		System.out.println("Java == "+mwr.reverseWord("Java"));
		System.out.println("program == "+mwr.reverseWord("program"));
	}
}

class StackImpl {

	private int stackSize;
	private char[] stackArr;
	private int top;

	/**
	 * constructor to create stack with size
	 * @param size
	 */
	public StackImpl(int size) {
		this.stackSize = size;
		this.stackArr = new char[stackSize];
		this.top = -1;
	}

	/**
	 * This method adds new entry to the top 
	 * of the stack
	 * @param entry
	 * @throws Exception 
	 */
	public void push(char entry) {
		this.stackArr[++top] = entry;
	}

	/**
	 * This method removes an entry from the 
	 * top of the stack.
	 * @return
	 * @throws Exception 
	 */
	public char pop() {
		char entry = this.stackArr[top--];
		return entry;
	}
	
	/**
	 * This method returns top of the stack
	 * without removing it.
	 * @return
	 */
	public char peek() {
		return stackArr[top];
	}

	/**
	 * This method returns true if the stack is 
	 * empty
	 * @return
	 */
	public boolean isStackEmpty() {
		return (top == -1);
	}

	/**
	 * This method returns true if the stack is full
	 * @return
	 */
	public boolean isStackFull() {
		return (top == stackSize - 1);
	}
}

Output:
Java2Novice == ecivoN2avaJ
Java == avaJ
program == margorp
<< Previous Program | Next Program >>
blog comments powered by Disqus

List of Stack Data Structure Examples

  1. Stack introduction & implementation
  2. Java Dynamic Stack Implementation
  3. Stack implementation using generics bounded type.
  4. Reverse a word or string using Stack data structure.
  5. Write a program to find out delimiter matching using stack.
  6. Convert a decimal into a binary number using stack.
  7. Towers of Hanoi implementation using stack.
  8. Evaluation of an infix expression that is fully parenthesized using stack in java.
Knowledge Centre
Procedural Vs Object-oriented Programs
In procedural program, programming logic follows certain procedures and the instructions are executed one after another. In OOP program, unit of program is object, which is nothing but combination of data and code.

In procedural program, data is exposed to the whole program whereas in OOPs program, it is accessible with in the object and which in turn assures the security of the code.
Famous Quotations
I respect faith, but doubt is what gets you an education.
-- Wilson Mizner

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.