实验三 String类的应用
实验目的
掌握类String类的使用;
学会使用JDK帮助文档;
实验内容

1.1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)

package test;

public class One {
    public static void main (String argsp[]) {
        String str = "this is a test of java";


        int count1 = 0;
        for(int i = 0; i < str.length();i++) {
            char c = str.charAt(i);
            if(c == 's')
                count1++;
        }
        System.out.println("s出现的次数为:"+count1);



        int count2 = 0;
        int index = 0;
        String ch = "is";
        while((index = str.indexOf(ch,index)) != -1) {
            index = index + ch.length();
            count2++;
        }
        System.out.println("子串\"is\"出现的次数为:"+count2);


        int count3 = 0;
        char a[] = str.toCharArray();
        for(int i = 0; i < str.length(); i++) {
            if(a[0] == 'i' && a[1] == 's' && a[2] == ' ')
                count3++;
            else if(a[i] == 'i' && a[i - 1] == ' ' && a[i + 1] == 's' && (a[i + 2] == ' '));
                count3++;
        }
        System.out.println("单词\" is\"出现的次数为:"+count3);


        String[] words = str.split(" ");
        System.out.print("倒叙:");
        for(int i = words.length-1; i >= 0; i--){
            System.out.print(words[i]+" ");
        }
    }
}

2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。

package test;
import java.util.Scanner;
public class Two {
    @SuppressWarnings("static-access")
    public static void main (String args[]) {


        System.out.println("加密请输入a,解密请输入b");

        Scanner operate = new Scanner(System.in);


        if(operate.nextLine().equals("b")) {
            String str = operate.nextLine();
            char b[] = str.toCharArray();
            for(int j = 0; j < str.length();j++) {
                b[j] = (char)(b[j] - 3);
            }
            str=str.valueOf(b);
            System.out.println(str);
        }

        else{
            String str = operate.nextLine();
            char b[] = str.toCharArray();
            for(int i = 0; i < str.length();i++) {
                b[i] = (char)(b[i] + 3);
            }
            str=str.valueOf(b);
            System.out.println(str);
        }
    }
}


3.已知字符串“ddejidsEFALDFfnef2357 3ed”。输出字符串里的大写字母数,小写英文字母数,非英文字母数。

package test;

public class Three {
        public static void main(String args[]) {

            String str = "ddejidsEFALDFfnef2357 3ed";

            char a[] = str.toCharArray();

            int count1 = 0, count2 = 0, count3 = 0;

            for(int i = 0; i <= str.length() - 1;i++){
                if(a[i] >= 'A' && a[i] <= 'Z'){
                    count1++;
                }

                else if(a[i] >= 'a' && a[i] <= 'z') {
                    count2++;
                }

                else {
                    count3++;
                }
            }

            System.out.println("大写字母数为:"+count1);
            System.out.println("小写字母数为:"+count2);
            System.out.println("非英文字母数为:"+count3);
        }
}
02-11 23:10