July 2018
Beginner
202 pages
5h 4m
English
We need to make use of a stack data structure for reversing a string.
Follow these steps:
public String reverse(String str)
public String reverse(String str) { StringBuilder result = new StringBuilder(); Stack<Character> stack = new Stack<>(); for (char c : str.toCharArray()) stack.push(c); Optional<Character> optChar = stack.pop(); while (optChar.isPresent()) { result.append(optChar.get()); optChar = stack.pop(); } return result.toString();}
Read now
Unlock full access