加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

第五周课程总结&试验报告(三)

发布时间:2020-12-15 07:33:31 所属栏目:Java 来源:网络整理
导读:已知字符串:"this is a test of java".按要求执行以下操作 统计该字符串中字母s出现的次数。 统计该字符串中子串“is”出现的次数。 统计该字符串中单词“is”出现的次数。 实现该字符串的倒序输出。 public class daoxu { public static void main(String[

已知字符串:"this is a test of java".按要求执行以下操作

统计该字符串中字母s出现的次数。
统计该字符串中子串“is”出现的次数。
统计该字符串中单词“is”出现的次数。
实现该字符串的倒序输出。

public class daoxu {
    public static void main(String[] args) {
        String s = "this is a test of java";
        int x = (s.split("s")).length - 1;
        System.out.println("字符串中字母s出现的次数" + x);
        
        int y = (s.split("is")).length - 1;
        System.out.println("字符串中子串“is”出现的次数" + y);
        
        int z = (s.split(" is")).length - 1;
        System.out.println("字符串中单词“is”出现的次数" + z);
        
        StringBuffer str = new StringBuffer("this is a test of java");
        System.out.println(str.reverse());      //使用了StringBuffer类中的reverse()方法
    }
}

2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串

import java. util.*;
public class jiami{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要加密的英文字串");
        String str = sc.nextLine();
        
        char n;
        String str1=new String();
        for(int i=0;i<str.length();i++) {
            n = str.charAt(i);
            n = (char)(n+3);       //字符后移3位
            
            str1+=n;
        }
        System.out.println("加密后的子串是:n"+str1);
    }
}

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

public class daxiao {
    public static void main(String[] args) {
        int x,y,z;
        x=y=z=0;
        String s = "ddejidsEFALDFfnef2357 3ed";
        char c[] = s.toCharArray();
        for(int i=0;i< c.length;i++) {
            if(c[i]>= 'a'&&c[i] <= 'z') {
                x++;
            }
            
            else if(c[i] >= 'A'&&c[i]<='Z') {
                y++;
            }
            
            else {
                z++;
            }
        }
        System.out.println("字符串里的大写英文字母数" +y);
        System.out.println("字符串里的小写英文字母数" +x);
        System.out.println("字符串里的非英文字母数" +z);
    }
    
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读