字符串综合练习

1. 转换罗马数字

需求:键盘录入一个字符串
要求1: 长度为小于等于9
要求2:只能是数字
将内容变为罗马数字
下面是阿拉伯数字跟罗马数字的对应关系“
Ⅰ-1、Ⅱ-2、Ⅲ-3、Ⅳ-4、Ⅴ-5、Ⅵ-6、Ⅶ-7、Ⅷ-8、Ⅸ-9
注意点:
罗马数字里面是没有0的
如果键盘录入0,可以变成"",长度为0的字符串

  • 方法1
import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str;
        while (true) {
            System.out.println("输入一个字符串:");
            str = sc.next();
            if (checkStr(str)) {
                break;

            } else {
                System.out.println("当前字符串不符合规则,请重新输入");
                continue;
            }
        }

        //字符串拼接
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            //int num = c - 48;
            int num = c - '0';
            sb.append(changeLuoMa(num));
        }
        System.out.println(sb);
    }

    public static boolean checkStr(String str) {
        if (str.length() > 9) {
            return false;
        }

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c < '0' && c > '9') {
                return false;
            }
        }
        return true;
    }

    public static String changeLuoMa(int number) {
        String[] str = {"", "Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ"};
        return str[number];
    }
}
  • 方法2(switch,不需要将字符转换成数字)
    //switch匹配
    public static String changeLuoMa(char number) {
        //JDK12 新特性,在switch前面可以加入变量接收
        String str = switch (number) {
            case '0' -> "";
            case '1' -> "Ⅰ";
            case '2' -> "Ⅱ";
            case '3' -> "Ⅲ";
            case '4' -> "Ⅳ";
            case '5' -> "Ⅴ";
            case '6' -> "Ⅵ";
            case '7' -> "Ⅶ";
            case '8' -> "Ⅷ";
            case '9' -> "Ⅸ";
            default -> "";
        };
        return str;
        //等价于
//        String str;
//        switch(number) {
//            case '0' -> str = "";
//            ……
//        }
//		return str;

		//等价于
//	      String str;
//        switch (number) {
//            case '0': {
//                str = "";
//                break;
//            }
//            case '1': {
//                str = "Ⅰ";
//                break;
//            }
//            case '2': {
//                str = "Ⅱ";
//                break;
//            }
//            case '3': {
//                str = "Ⅲ";
//                break;
//            }
//            case '4': {
//                str = "Ⅳ";
//                break;
//            }
//            case '5': {
//                str = "Ⅴ";
//                break;
//            }
//            case '6': {
//                str = "Ⅵ";
//                break;
//            }
//            case '7': {
//                str = "Ⅶ";
//                break;
//            }
//            case '8': {
//                str = "Ⅷ";
//                break;
//            }
//            case '9': {
//                str = "Ⅸ";
//                break;
//            }
//            default: {
//                str = "";
//            }
//        }
//        return str;
    }

2. 调整字符串

需求:给定两个字符串,A和B
A的旋转操作就是把A最左边的字符移到最右边
例如 A ='abcde',移动一次之后结果就是A ='bcdea'
如果若干次调整操作之后,A能变成B,那么返回true。否则返回false

  • 方法1:substring()截取,再把左边的字符拼到右侧
public class Test2 {
    public static void main(String[] args) {
        String strA = "abcde";
        String strB = "deabc";
        boolean result = check(strA, strB);
        System.out.println(result);
    }

    public static boolean check(String strA, String strB) {
        for (int i = 0; i < strA.length(); i++) {
            strA = rotate(strA);
            if (strA.equals(strB)) {
                return true;
            }
        }
        return false;
    }

    public static String rotate(String str) {
        return str.substring(1) + str.charAt(0);
    }
}
  • 方法2:先把字符串变成一个字符数组char[] arr = str.toCharArray(),然后调整字符数组里面的数据,最后再把字符数组变成字符串
  public static String rotate(String str) {
        //"ABC" -> ['A','B','C'] -> ['B','C','A'] ->new String(数组)变回字符串
        char[] arr = str.toCharArray();
        char first = arr[0];
        for (int i = 1; i < arr.length; i++) {
            arr[i-1] = arr[i];
        }
        arr[arr.length - 1] = first;
        String result = new String(arr);
        return result;
    }

3. 打乱字符串内容

需求:键盘录入任意字符串,打乱里面的内容
只能用第二题的方法2:先把字符串变成一个字符数组char[] arr = str.toCharArray(),然后打乱字符数组里面的内容,最后再把字符数组变回字符串

import java.util.Random;
import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个字符串");
        String str = sc.nextLine();

        char[] arr = str.toCharArray();

        for (int i = 0; i < arr.length; i++) {
            Random r = new Random();
            int num = r.nextInt(arr.length);
            char temp = arr[i];
            arr[i] = arr[num];
            arr[num] = temp;
        }
        String result = new String(arr);
        System.out.println(result);
    }
}

4. 生成验证码

需求:内容可以是小写字母,也可以是大写字母,还可以是数组

  • 要求:
    长度为5,内容中是四位字母,1位数字。
    其实数字只有一位,但是可以出现在任意位置
import java.util.Random;

public class Test4 {
    public static void main(String[] args) {
        char[] letterArr = new char[52];
        for (int i = 0; i < 26; i++) {
            letterArr[i] = (char) ('a' + i);
        }
        for (int i = 26; i < letterArr.length; i++) {
            letterArr[i] = (char) ('A' + i - 26);
        }

        Random r = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int num = r.nextInt(letterArr.length);
            char c = letterArr[num];
            sb.append(c);
        }

        int num = r.nextInt(10);
        sb.append(num);

        char[] arr = sb.toString().toCharArray();

        int randomNum = r.nextInt(arr.length);
        char temp = arr[randomNum];
        arr[randomNum] = arr[arr.length - 1];
        arr[arr.length - 1] = temp;

        String result = new String(arr);
        System.out.println(result);
    }
}

5. 字符串数字相乘

需求:给定两个字符串形式表示的非负整数num1和num2,返回num1和num2的乘积,他们的乘积也表示为字符串形式

public class Test5 {
    public static void main(String[] args) {
        String s1 = "123";
        String s2 = "45";

        int num = StringToNumber(s1) * StringToNumber(s2);
        String str = NumberToString(num);
        System.out.println(str);

    }

    public static int StringToNumber(String str) {
        char[] c = str.toCharArray();
        int num = 0;
        for (int i = 0; i < c.length; i++) {
            num = num * 10 + c[i] - '0';
        }
        return num;
    }

    public static String NumberToString(int num) {
        StringBuilder sb = new StringBuilder();
        while (num != 0) {
            int ge = num % 10;
            char cge = (char) (ge + 48);
            sb.append(cge);
            num = num / 10;
        }
        String result = sb.reverse().toString();
        return result;
    }
}

6. 输出最后一个单词的长度

需求:给定一个字符串s,由若干单词组成,单词前后用一些空格字符隔开。
返回字符串中最后一个单词的长度
单词是指由字母组成、不包含任何空格字符的最大字字符串

示例1:输入s = "Hello World" 输出:5
示例2:输入s = " fly me to the moon" 输出:4
示例1:输入s = "luffy is still joyboy" 输出:6

public class Test6 {
    public static void main(String[] args) {
        String s = " fly me  to  the moon   ";
        char[] chs = s.toCharArray();
        int count = 0;
        int i = chs.length - 1;
        while (chs[i] == ' ') {
            i--;
        }
        while (chs[i] != ' ') {
            count++;
            i--;
        }
        System.out.println(count);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值