题解 | #字符串反转#
字符串反转
http://www.nowcoder.com/practice/e45e078701ab4e4cb49393ae30f1bb04
import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); // 声明一个栈用于存放元素。 Stack<Character> stack = new Stack<Character>(); // 全部入栈 for (char ch : str.toCharArray()) { stack.push(ch); } // 全部出栈 StringBuffer newstr = new StringBuffer(); for (int i = 0; i < str.length(); i++) { char a = stack.pop(); newstr.append(a); } System.out.println(newstr); } }