StringDemo5

package cn.zuoye;
import java.util.Scanner;

/*  字符串反转
 * 举例:录入:123
 * 输出:321
 *
 * 分析:键盘录入
 *   定义一个新字符串
 *   倒着遍历字符串,得到每个字符串
 *   用新字符串把每一个字符拼接起来
 *   输出
 *
 */
public class StringDemo5 {
 public static void main(String[] args) {
  // 键盘录入
  Scanner sc = new Scanner(System.in);
  System.out.println("请输入一个字符串:");
  String line = sc.nextLine();
  String ss= StringDemo5.Myrr(line);
  System.out.println(ss);
  /*// 定义一个新字符串
  String result = "";
  // 字符串转换成字符数组
  char[] chs = line.toCharArray();
  
  //倒着遍历字符串,得到每个字符串
  for(int x=chs.length-1;x>=0;x--){
   //用新字符把每一个字符拼接起来
   result +=chs[x];
  }*/
  //输出
 // System.out.println(result);
  
 //功能版
 /*
  * 返回值:String
  * 参数类型:String 
  */
 
}
 public static String Myrr(String s){
  String result = "";
  // 字符串转换成字符数组
  char[] chs = s.toCharArray();
  
  //倒着遍历字符串,得到每个字符串
  for(int x=chs.length-1;x>=0;x--){
   //用新字符把每一个字符拼接起来
   result +=chs[x];
  } 
  return result;
}
}
原文地址:https://www.cnblogs.com/rong123/p/9894462.html