String的一些练习题目

1、检查输入的字符串是否为”回文”,返回boolean结果。2、convert2Array()方法将传入的字符串转换为字符数组返回。提示:不能使用toCharArray() 方法。3、countX()方法统计参数字符串中字符x出现的次数,返回int结果。4、dispalyCount()统计传入的字符串中的字母、数字以及其它字符的个数,并显示。5、convertAndSqrt()将传入的数字字符串转换为数值,返回该数值的平方根。将、6入的一个语句,进行单词内的反向输出 例如:I love this game 输出: I evol siht emag

package com.My.chapter01;

import javax.swing.JOptionPane;

public class TestString02 {
    private static int index;

    public static void main(String[] args) {

        String str;
        isPalindrome("avff");
        convert2Array("122133dfdf");
        countX("abcxaaax");
        dispalyCount("123cxddgj**^%");
        convertAndSqrt("123");
        Reverse("I love this game");
    }

    // 2、isPalindrome ()方法能够检查输入的字符串是否为”回文”,返回boolean结果。
    public static boolean isPalindrome(String a) {
        boolean flag = true;
        for (int i = 0; i < a.length(); i++) {
            if (a.charAt(i) == a.charAt(a.length() - i - 1)) {
                flag = true;
            } else {
                flag = false;
            }
        }
        System.out.println(flag);
        System.out.println();
        return flag;
    }

    // 3,convert2Array()方法将传入的字符串转换为字符数组返回。提示:不能使用toCharArray() 方法。
    public static char[] convert2Array(String a) {

        char b[] = new char[a.length()];
        for (int i = 0; i < a.length(); i++) {
            b[i] = a.charAt(i);
        }
        System.out.println(b);
        System.out.println();
        return b;

    }

    // 4,countX()方法统计参数字符串中字符x出现的次数,返回int结果。
    public static int countX(String a) {
        int count = 0;
        for (int i = 0; i < a.length(); i++) {
            if (a.charAt(i) == 'x') {
                count++;
            }
        }
        System.out.println("字符串中的x个数为:  " + count);
        return count;

    }

    // 5,dispalyCount()统计传入的字符串中的字母、数字以及其它字符的个数,并显示。
    public static void dispalyCount(String a) {
        int x = 0; // 统计字母个数
        int y = 0; // 统计数字个数
        int z = 0; // 统计其他字符个数
        char charNumber[] = new char[a.length()];
        int number[] = new int[a.length()];
        char elseNumber[] = new char[a.length()];
        for (int i = 0; i < a.length(); i++) {
            char b = a.charAt(i);
            if (b >= 48 && b <= 56) {
                number[y] = b - 48;
                y++;
            } else if ((b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z')) {
                charNumber[x] = b;
                x++;
            } else {
                elseNumber[z] = b;
                z++;
            }

        }
        System.out.print("字母个数为: " + x + "   ");
        System.out.print("字符串中的字母为 : ");
        for (int i = 0; i < charNumber.length; i++) {
            System.out.print(charNumber[i] + "  ");
        }

        System.out.println();
        System.out.print("数字个数为: " + y + "   ");
        System.out.print("字符串中的数字为 : ");
        for (int i = 0; i < y; i++) {
            System.out.print(number[i] + "  ");
        }

        System.out.println();
        System.out.print("其他字符个数为: " + z + "   ");
        System.out.print("字符串中的其他字符为 : ");
        for (int i = 0; i < elseNumber.length; i++) {
            System.out.print(elseNumber[i] + "  ");
        }
        System.out.println();
        System.out.println();

    }

    // 6,convertAndSqrt()将传入的数字字符串转换为数值,返回该数值的平方根。
    public static double convertAndSqrt(String a) {
        double result = 0;
        if (a.equals(0)) {
            result = 0;
        } else {
            int x = Integer.parseInt(a);
            result = Math.sqrt(x);
            System.out.println(x + " 的平方根为  " + (int) (result * 100 + 0.5) / 100.0);

        }
        return result;

    }

    // 练习: 将输入的一个语句,进行单词内的反向输出 例如:I love this game 输出: I evol siht emag
    public static void Reverse(String a) {
        String str="   I love this game";
        str=str.trim()+" ";

        //利用空格个数 ,计数单词
        int  count = 0;
        int index = str.indexOf(" ");
        while(index != -1){
            count++;
            index=str.indexOf(" ",index+1);
        }


        //定义新数组放每个单词
        index = 0;
        int start = 0;
        int end = str.indexOf(" ");
        String newStr[]=new String[count];
        while(end != -1){
            newStr[index++] = str.substring(start,end);
            end = str.indexOf(" ",end+1);
        }



    }

}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值