Java Reverse String Using Stack

Chapter: String Handling Last Updated: 24-10-2016 18:02:59 UTC

Program:

            /* ............... START ............... */
                
import java.io.IOException;

public class JavaReverseStringUsingStack {

	private String input;
	private String output;

	public JavaReverseStringUsingStack(String in) {
		input = in;
	}

	public String doRev() {
		int stackSize = input.length();
		Stack theStack = new Stack(stackSize);
		for (int i = 0; i < input.length(); i++) {
			char ch = input.charAt(i);
			theStack.push(ch);
		}
		output = "";
		while (!theStack.isEmpty()) {
			char ch = theStack.pop();
			output = output + ch;
		}
		return output;
	}

	public static void main(String[] args) throws IOException {
		String input = "Welcome to JavaScan.com";
		String output;
		JavaReverseStringUsingStack theReverser = new JavaReverseStringUsingStack(input);
		output = theReverser.doRev();
		System.out.println("Reversed: " + output);
	}

	class Stack {
		private int maxSize;
		private char[] stackArray;
		private int top;

		public Stack(int max) {
			maxSize = max;
			stackArray = new char[maxSize];
			top = -1;
		}

		public void push(char j) {
			stackArray[++top] = j;
		}

		public char pop() {
			return stackArray[top--];
		}

		public char peek() {
			return stackArray[top];
		}

		public boolean isEmpty() {
			return (top == -1);
		}
	}
}

                /* ............... END ............... */
        

Output

Reversed: moc.nacSavaJ ot emocleW

Tags

Reverse String Using Stack, Java, String

Similar Programs Chapter Last Updated
Java StringBuffer CharAt String Handling 27-05-2017
Java StringBuilder EnsureCapacity String Handling 27-05-2017
Java StringBuffer Capacity Example String Handling 27-05-2017
Java Character At Particular Index String Handling 16-05-2017
Java Convert String To Array Example String Handling 16-05-2017
Java String Trim Method String Handling 15-05-2017
Java String Character Case Check String Handling 15-05-2017
Java Find All Substrings Of String String Handling 14-05-2017
Java String Conversion Using ValueOf String Handling 30-03-2017
Immutable String In Java String Handling 24-03-2017
Java Float To String Using toString() String Handling 05-11-2016
Java Program To Convert Integer To String Using toString() String Handling 05-11-2016
Java Integer To String Conversion String Handling 21-10-2016
Java Program To Check Isomorphic Strings String Handling 19-10-2016
Java String Unicode Code Point Example String Handling 28-08-2016
Concatenate Strings In Java Example String Handling 21-08-2016
String Optimization In Java String Handling 21-08-2016
Java String Split Example String Handling 20-08-2016
Java String Search String Handling 20-08-2016
Java String Replace String Handling 20-08-2016
Java String Remove Character Example String Handling 20-08-2016
Java String Last Occurance String Handling 20-08-2016
Java String Compare Example String Handling 20-08-2016
String Initialization In Java Example String Handling 20-06-2016
StringBuffer SubString In Java String Handling 09-05-2016
StringBuffer Reverse In Java String Handling 09-05-2016
StringBuffer Insert In Java String Handling 09-05-2016
StringBuffer Append In Java String Handling 09-05-2016
Java String isEmpty() String Handling 01-05-2016
StringBuilder reverse() Method In Java String Handling 01-05-2016

1 2 3