Java面试笔试提之字符串反转

来源:互联网 发布:画框软件 编辑:程序博客网 时间:2024/06/08 05:07

传智播客


package cn.itcast_07;

import java.util.Scanner;

/*
 * 字符串反转
 * 举例:键盘录入”abc”        
 * 输出结果:”cba”
 *
 * 分析:
 *         A:键盘录入一个字符串
 *         B:定义一个新字符串
 *         C:倒着遍历字符串,得到每一个字符
 *             a:length()和charAt()结合
 *             b:把字符串转成字符数组
 *         D:用新字符串把每一个字符拼接起来
 *         E:输出新串
 */
public class StringTest3 {
    public static void main(String[] args) {
        // 键盘录入一个字符串
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String line = sc.nextLine();

        /*
        // 定义一个新字符串
        String result = "";

        // 把字符串转成字符数组
        char[] chs = line.toCharArray();

        // 倒着遍历字符串,得到每一个字符
        for (int x = chs.length - 1; x >= 0; x--) {
            // 用新字符串把每一个字符拼接起来
            result += chs[x];
        }

        // 输出新串
        System.out.println("反转后的结果是:" + result);
        */

        // 改进为功能实现
        String s = myReverse(line);
        System.out.println("实现功能后的结果是:" + s);
    }

    /*
     * 两个明确: 返回值类型:String 参数列表:String
     */
    public static String myReverse(String s) {
        // 定义一个新字符串
        String result = "";

        // 把字符串转成字符数组
        char[] chs = s.toCharArray();

        // 倒着遍历字符串,得到每一个字符
        for (int x = chs.length - 1; x >= 0; x--) {
            // 用新字符串把每一个字符拼接起来
            result += chs[x];
        }
        return result;
    }
}



请输入一个字符串:
aseadasda
实现功能后的结果是:adsadaesa


自己的写法:

package lianxi;


import java.util.Scanner;


public class sa {


public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
String aa=sc.next();
char[] bb= aa.toCharArray();
String result="";
for(int i=bb.length-1;i>=0;i--){
result+= bb[i];
}
System.out.println(result);
}
}



0 0