华为机试-字符逆序

题目描述
将一个字符串str的内容颠倒过来,并输出。str的长度不超过100个字符。 如:输入“I am a student”,输出“tneduts a ma I”。

输入参数:
inputString:输入的字符串

返回值:
输出转换好的逆序字符串

输入描述:
输入一个字符串,可以有空格


输出描述:
输出逆序的字符串

输入例子:
I am a student

输出例子:
tneduts a ma I

Java程序实现

  1. import java.util.Scanner;  
  2. import java.util.Stack;  
  3.   
  4. /** 
  5.  * 字符逆序 
  6.  *  
  7.  * @author WWJ 
  8.  * 
  9.  */  
  10. public class Main {  
  11.   
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub  
  14.         Scanner sc = new Scanner(System.in);  
  15.         Stack<Character> stack = new Stack<>();  
  16.         while (sc.hasNext()) {  
  17.             String string = sc.nextLine();  
  18.             for (int i = 0; i < string.length(); i++) {  
  19.                 stack.push(string.charAt(i));  
  20.             }  
  21.             while (!stack.isEmpty()) {  
  22.                 System.out.print(stack.pop());  
  23.   
  24.             }  
  25.             System.out.println(" ");  
  26.         }  
  27.     }  
  28.   
  29. }  
原文地址:https://www.cnblogs.com/wwjldm/p/7097271.html