蓝桥杯 算法训练 字符串变换

蓝桥杯 算法训练 字符串变换_第1张图片


import java.util.Scanner;
public class Main {

    public static void change1(String str) {
        System.out.println(str.toUpperCase());
    }

    public static void change2(String str) {
        System.out.println(str.toLowerCase());
    }

    public static void change3(String str) {
        char[] arr = str.toCharArray();
        for(int i = arr.length - 1;i >= 0;i --) {
            System.out.print(arr[i]);
        }
        System.out.println();
    }

    public static void change4(String str) {
        String t = "";
        char[] arr = str.toCharArray();
        for(int i = 0;i < arr.length;i ++) {
            if(arr[i] <= 90 && arr[i] >=65) {
                t += (char)(arr[i] + 32);
            }else {
                t += (char)(arr[i] - 32);
            }
        }
        System.out.println(t.trim());
    }

    public static void change5(String str) {
        String t = "";
        str = str.toLowerCase();
        char[] arr = str.toCharArray();
        for(int i = 0; i < arr.length; i++) {
            if(i == 0) {
                t += arr[0];
            } else if(i != arr.length - 1) {
                if(arr[i] != arr[i - 1] + 1 || arr[i] != arr[i + 1] - 1) {
                    t += arr[i];
                } else if(arr[arr.length - 1] != '-') {
//                  System.out.println(t.charAt(t.length() - 1));
                    if(t.charAt(t.length() -1) != '-') {
                        t += '-';
                    }else {
                        continue;
                    }   
                }
            } else {
                t += arr[i];
            }
        }
        System.out.println(t.trim());
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        String str = sc.nextLine();
        if(num == 1) {
            change1(str);
        }else if(num == 2) {
            change2(str);
        }else if(num == 3) {
            change3(str);
        }else if(num == 4) {
            change4(str);
        }else {
            change5(str);
        }
    }
}

你可能感兴趣的:(java)