String-字符串反转

package cn.lianxi;

import java.util.Scanner;
/*
 * 
 * 字符串反转
 * 举例:键盘录入 “abc”
 * 结构:输出“cba”
 * 
 * 分析:1.键盘录入一个字符串
 *          2.定义一个新字符串
 *         3.倒着遍历字符串,得到每一个字符
 *                 a.length()和charAt()结合
 *                 b.把字符串转换成字符数组
 *         4,用新字符串把每一个字符拼接起来
 *         5输出新字符串
 * */
public class FanZhan {
    public static void main(String[] args) {
        /*
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入字母:");
        String s = sc.nextLine();
        String result = "";
        char[] chs = s.toCharArray();
        for(int i = chs.length-1;i>=0;i--){
            result += chs[i];
        }
        System.out.println(result);*/
        
        
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入字母:");
        String s = sc.nextLine();
        String result = "";
        for(int i =s.length()-1;i>=0;i--){
            result += s.charAt(i);
        }
        System.out.println(result);
            
    }
    
}

 

posted @ 2015-12-23 01:14  Deleting  阅读(660)  评论(0编辑  收藏  举报