Experimental Java Report III

1. 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 appears ① statistics.

The number of neutrons ② string string "is" appears in the statistics.

The number of words in the string "is" appears ③ statistics.

④ realize reverse output of the string

   (1)

package demo;

public class text {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "this is a test of java";
        int sum = str.replaceAll("[^s]", "").length();
        System.out.println(sum);
    }

}

 

  (2) 

package demo;

public class text {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "this is a test of java";
        String s = "is";
        String[] arr = (","+str.toLowerCase()+",").split(s);
        System.out.println(arr.length - 1);
    }

}

 

 

 (3)

 

(4)

package demo;

public class text {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "this is a test of java";
        StringBuffer s = new StringBuffer(str);
        System.out.println(s.reverse().toString());
        StringBuffer sb2 = new StringBuffer();
        for (int i = str.length()-1; i >=0; i--) {
            s.append(str.charAt(i));
        }
        System.out.println(sb2);
    }
}

 

 

 

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

 

 

 

 

 Code

package demo;

public class demo1 {

    public static void main(String[] args) {
                String str = "20188492";
                System.out.println(demo1.moveToRight(str, 3));
    }

private static String moveToRight(String str,int position) {
    String str1=str.substring(str.length()-position);
    String str2=str.substring(0, str.length()-position);
    return str1+str2;
}
}

 

 

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

 

 Code

package demo;

public class demo1 {

    public static void main(String[] args) {
                String s = "ddejidsEFALDFfnef2357 3ed";
                int capital = 0;
                int lowercase = 0;
                int numberCount = 0;
         
                for (int i = 0; i < s.length(); i++) {
                    char ch = s.charAt(i);
                    if (ch >= 'a' && ch <= 'z') {
                        lowercase ++;
                    } The else  IF (CH> = 'A' && CH <= 'the Z' ) { 
                        Capital ++ ; 
                    } the else  IF (CH> = '0' && CH <= '. 9' ) { 
                        numberCount ++ ; 
                    } 
                } 
                
                the System. Out.println ( "capital letters" + capital + "a" ); 
                System.out.println ( "lowercase" + lowercase + "a" ); 
                System.out.println ( "digital" + numberCount + "th" ) ;

    }

}

 

Guess you like

Origin www.cnblogs.com/chenguohhw/p/11544789.html