JAVA autumn 2019 the third week of the course and test summary report (b)

personal blog

A basic string manipulation

Title: Known string: "this is a test of java " required to do the following requirements :( source code, the results screenshot).

  • The number of letters in the string s occurs statistics.
  • The string neutron count the number of times the string "is" appears.
  • The number of words in the string "is" appears in the statistics.
  • Achieve reverse the string is output.
package com.company;

public class Main {
    static String str = "this is a test of java";

    public static void main(String[] args) {
        query("s","该字符串中字母s出现的次数。");
        query("is","该字符串中子串“is”出现的次数。");
        query( " is ","该字符串中单词“is”出现的次数。");

        resultString();
    }

    public static void query(String str1, String sentence) {
        int count = 0;
        int temp = str.indexOf(str1);
        while(temp >= 0 && temp <= str.length()) {
            temp= str.indexOf(str1,temp+1);
            count++;
        }
        System.out.println(sentence+count);
    }

    public static void resultString() {
        String resultString = "";
        char[] charArray = str.toCharArray();

        for (int i=charArray.length-1; i>=0; i--){
            resultString += charArray[i];
        }

        System.out.println(resultString);
    }
}

Second, encryption

Title: Write a program using the following algorithm to encrypt or decrypt English string entered by the user. It requires source code, the results screenshot.

Encryption process

package com.company;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        
        String s = "";
        char[] charArray = str.toCharArray();
        
        for (int i = 0; i < charArray.length; i++) {
            s += (char)((int)charArray[i]+3);
        }
        System.out.println(s);
    }
}

Third, the classification of characters

Topic: Known string "ddejidsEFALDFfnef2357 3ed". The output string in the number of capital letters, lowercase letters count, the number of non-English letters.

package com.company;

public class Main {
    public static void main(String[] args) {
        String str = "ddejidsEFALDFfnef2357 3ed";

        char[] charArray = str.toCharArray();           // 字符串 -> 字符数组

        String str1="";
        String str2="";
        String str3="";

        for (int i = 0; i < charArray.length; i++) {
            if(Character.isUpperCase(charArray[i]))         // 大写
                str1+=charArray[i];
            else if(Character.isLowerCase(charArray[i]))    // 小写
                str2+=charArray[i];
            else        // 非字母
                str3+=charArray[i];
        }

        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
    }
}

Guess you like

Origin www.cnblogs.com/JingWenxing/p/11595271.html