StackDemo:
import java.util.Stack; public class StackDemo { public static void main(String[] args) { // TODO Auto-generated method stub String str = "+u+n+c---+e+r+t---+a-+i-+n+t+y---+-+r+u--+l+e+s---"; Stack<Character> stack = new Stack<>(); String s = ""; char[] ch = str.toCharArray(); for (int i=0;i<ch.length;i++) { if (ch[i] == '+') { stack.push(ch[i+1]); i++; }else { s += stack.pop()+""; } } // s += stack.pop(); System.out.println(s); } }