第11次作业--字符串处理

题目一

  1. 题目

     编写一个应用程序,统计输入的一个字符串中相同字符的个数,并将统计结果输出。

  2. 源程序

    Test.java

     

     1 /**
     2  * 文件功能:统计输入的一个字符串中相同字符的个数,并将统计结果输出。
     3  */
     4 import java.util.*;
     5 
     6 public class Test {
     7 
     8     public static void main(String[] args) {
     9         Scanner in = new Scanner(System.in);
    10         System.out.println("输入字符串:");
    11 
    12         String str1 = in.next();
    13         char str2[] = new char[str1.length()];
    14 
    15         duplicationRemoval dr = new duplicationRemoval();
    16         dr.DR(str1, str2);// 字符串str1去重,并存储到字符数组str2中
    17 
    18         duplicationCheck dc = new duplicationCheck();
    19         dc.DC(str1, str2, dr.k);//查重复并输出
    20         
    21     }
    22 
    23 }

     

    duplicationRemoval.java

     

     1 /**
     2  * 1.文件功能:统计字符串str1中相同字符的个数存储到str2中
     3  * 2.成员变量:k 重复字符的个数+1
     4  * 3.方法:DR(String str1, char[] str2),接受一个字符串和一个字符数组
     5  */
     6 public class duplicationRemoval {
     7     int k;
     8 
     9     void DR(String str1, char[] str2) {
    10         k = 0;
    11 
    12         for (int i = 0; i < str1.length(); i++) {
    13             int count = 0;
    14 
    15             if (i > 0) {
    16                 for (int j = 0; j < i; j++) {
    17                     if (str1.charAt(i) == str1.charAt(j)) {
    18                         count++;
    19                     }
    20                 }
    21             }
    22 
    23             if (count != 0)
    24                 continue;
    25 
    26             str2[k++] = str1.charAt(i);
    27 
    28         }
    29 
    30     }
    31 }

     

    duplicationCheck.java

     

     1 /**
     2  * 1.文件功能:输出字符串str1中相同字符的个数
     3  * 2.成员变量:无
     4  * 3.方法:void DC(String str1,char[] str2,int k)接受字符串、字符数组和重复字符的个数
     5  */
     6 public class duplicationCheck {
     7     void DC(String str1,char[] str2,int k) {
     8         for (int i = 0; i < k; i++) {
     9             int count = 0;
    10             System.out.print(str2[i]+" ");
    11             
    12             for(int j=0;j<str1.length();j++) {
    13                 if(str1.charAt(j)==str2[i]) {
    14                     count++;
    15                 }
    16             }
    17             System.out.println("出现了"+count+"次");
    18         }
    19     }
    20 }

     

  3. 运行结果

     

题目二

  1. 题目

     编写程序,输入一个字符串,判断该串中的字母能否组成一个回文串(回文串:一个字符串从前向后读取和从后向前读取都一样)。如:ab<c>c?ba

  2. 源程序

    Test.java

     1 import java.util.*;
     2 
     3 public class Test {
     4 
     5     public static void main(String[] args) {
     6         Scanner in = new Scanner(System.in);
     7         String str = in.nextLine();
     8         
     9         str = str.replaceAll("[^a-z^A-Z]", "");
    10         
    11         StringBuffer str2 = new StringBuffer(str);
    12         if(str2==str2.reverse()) {
    13             System.out.println("回文");
    14         }else {
    15             System.out.println("不回文");
    16         }
    17         
    18     }
    19 }

     

  3. 运行结果

     

posted @ 2019-11-19 18:18  张博涵  阅读(145)  评论(0)    收藏  举报