zoukankan      html  css  js  c++  java
  • 第五周

    实验三 String类的应用

    实验目的

    掌握类String类的使用;

    学会使用JDK帮助文档;

    实验内容

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

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

    public class zuoye01 {
        
        String s = "this is a test of java";
        static Test5 OneString = new Test5();
        public static void main(String[] args) {
            OneString.numberS();
            OneString.numberIS();
            OneString.numberwordIS();
            OneString.reversal();
        }
        public void numberS() {
            int number = 0;
            for(int i = 0 ; i < s.length(); i++) {
                char c = s.charAt(i);
                if(c=='s') {
                    number++;
                }
            }
            System.out.println("S出现次数"+number);
        }
        public void numberIS() {
            int number = 0;
            for(int i = 1 ; i < s.length() ; i++) {
                char c = s.charAt(i-1);
                char c1 = s.charAt(i);
                if(c=='i'&&c1=='s') {
                    number++;
                }
            }
            System.out.println("字符IS出现次数"+number);
        }
        public void numberwordIS() {
            int number = 0;
            String s[] = this.s.split(" ");
            for(String s1 : s) {
                if(s1.equalsIgnoreCase("is")) {
                    number++;
                }
            }
            System.out.println("单词IS"+number);
        }
        public void reversal() {
            StringBuilder word  = new StringBuilder();
            for(int i = s.length()-1 ; i>=0 ; i--) {
                char c = s.charAt(i);
                word = word.append(s.charAt(i));
            }
            System.out.println("倒序输出 " + word);
        }
    }
    
    

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

    import java.util.Scanner;
    public class zuoye02 {
        
        static Scanner scanner = new Scanner(System.in);
        public static void main(String[] args) {
            StringBuilder s = new StringBuilder();
            String s2 = " ";
            String s1 = scanner.next();
            for(int i = 0 ; i < s1.length() ; i++) {
                int c = s1.charAt(i);
                if(c>=65 && c<=90) {
                    if(c==88) {
                        c = 65;
                    }else if(c==89) {
                        c = 66;
                    }else if(c==90) {
                        c = 67;
                    }else {
                        c+=3;
                    }
                    s2 = String.valueOf(s.append((char)c));
                }else
                if(c>=97 && c<=122) {
                    if(c==120) {
                        c = 97;
                    }else if(c==121) {
                        c = 98;
                    }else if(c==122) {
                        c = 99;
                    }else {
                        c+=3;
                    }
                    s2 = String.valueOf(s.append((char)c));
                }
            }
            System.out.println("加密前 "+s1);
            System.out.println("加密后 "+s);
        }
    
    }
    

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

    public class zuoye03 {
    
        static String s = "ddejjdsEFALDFfnef2357 3ed";
        public static void main(String[] args) {
            int Word = 0;
            int word = 0;
            int other = 0;
            for(int i = 0;i < s.length();i++) {
                char c = s.charAt(i);
                if(c>='A' && c<='Z') {
                    Word++;
                }else 
                if(c>='a' &&c<='z') {
                    word++;
                }else {
                    other++;
                }
            }
            System.out.print("大写字母: "+Word+",小写字母 "+word+",其他 "+other);
        }
    
    }
    

  • 相关阅读:
    30 Day Challenge Day 20 | Leetcode 938. Range Sum of BST
    30 Day Challenge Day 20 | Leetcode 124. Binary Tree Maximum Path Sum
    30 Day Challenge Day 20 | Leetcode 94. Binary Tree Inorder Traversal
    idea2019版本破解
    xml文件时第一行无缘无故报错
    activeMQ的基本使用
    activeMQ的安装(Linux下)
    redis的基本用法
    redis安装(Linux下)
    redis安装(window下)
  • 原文地址:https://www.cnblogs.com/xia-unun/p/11600420.html
Copyright © 2011-2022 走看看