Fifth week of learning & test report summary (c)

experimental report

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

(1) count the number of letters in the string s appears.

package shyzye;

public class shsan {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str="this is a test of java";
        int count=0;
        char[] c=str.toCharArray();
        for(int i=0;i<c.length;i++) {
            if(c[i]=='s') {
                count++;
            }
        }

        System.out.println("S的个数:"+count);
    }
    

}

(2) count the number of words in the string "is" appears.

package shyzye;

public class shsan2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String str="this is a test of java";
        int count=0;
        
        String[] c=str.split(" ");
        for(String e:c) {
            if(e.equals("is")) {
                count++;
            }
        }
        System.out.println("is作为单词出现的次数:"+count);
    }

}

(3) count the number of times the string appears neutron string "is".

package shyzye;

public class shsan3 {
    public static void main(String[] args) {
        String str="this is a test of java";
        int i=0;
        int count=0;
        while(str.indexOf("is",i)!=-1){
            count++;
            i=str.indexOf("is",i)+1;
            
        }
        System.out.println("is作为字符出现的次数:"+count);
    }

}

(4) achieve reverse the string is output.

package shyzye;

public class shsan4 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        StringBuffer str=new StringBuffer();
        str.append("this is a test of java");
        System.out.print("倒序:"+str.reverse());
    }

}

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.

package shyzye;

import java.util.*;

public class shsan二 {

    public static void main(String[] args) {
        
        int i,j=0;
        
        Scanner scanf=new Scanner(System.in);
        String str=scanf.nextLine();
        
        char chr[]=str.toCharArray();
        char[] chr1=null;
        chr1 = new char[str.length()];
        
        for(i=chr.length-3;i<chr.length;i++) {
            
                chr1[j]=chr[i];
                j++;
                
           }
            for(i=0;i<chr.length-3;i++){

                chr1[j]=chr[i];
                j++;
                
            }
        
        System.out.print(chr1);
    }
}

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

package shyzye;

public class shsans {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str1="ddejidsEFALDFfnef2357 3ed";
        int count1=0,count2=0,count3=0;
        char[] up=str1.toCharArray();
        for(int i=0;i<up.length;i++) {
            if(up[i]>='a'&&up[i]<='z') {
                
                count1++;
               }
            else if(up[i]>='A'&&up[i]<='Z') {
                
                count2++;
            }
            else {
                
                count3++;
            }
          }
        System.out.println("小写英文字母:"+count1);
        System.out.println("大写英文字母:"+count2);
        System.out.println("其他字符:"+count3);
        
    }

}

Guess you like

Origin www.cnblogs.com/lyl68/p/11595066.html