第11次作业--字符串处理
题目1:
编写一个应用程序,统计输入的一个字符串中相同字符的个数,并将统计结果输出。
代码:
import java.util.Scanner; public class Statistics { public static int[] jishu(String str) { int[] a = new int[26]; int zz=0; String st = str.replaceAll(" ", ""); for(int i = 0; i < st.length(); i++) { char c = st.charAt(i); if(c>='a'&&c<='z') { zz = c - 'a'; }else if(c>='A'&& c<='Z'){ zz=c-'A'; } a[zz] = a[zz] + 1; } return a; } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("输入统计的字符串:"); String str = new Scanner(System.in).nextLine(); int[] b = jishu(str); for(int i = 0; i < b.length; i++) { if(b[i] != 0) { System.out.print((char) (i + 'a')+":"+b[i]+"\t"); } } } }
测试截图:

题目2:
编写程序,输入一个字符串,判断该串中的字母能否组成一个回文串(回文串:一个字符串从前向后读取和从后向前读取都一样)。如:ab<c>c?ba
代码:
import java.util.Scanner; public class panduan { public static boolean Judge (String st){ if(st.length()<=1){ return true; }else if(st.charAt(0) != st.charAt(st.length()-1)){ return false; } return Judge(st.substring(1,st.length()-1)); } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("输入一个字符串"); String st = new Scanner(System.in).nextLine(); System.out.println(Judge(st)); } }
测试截图:


浙公网安备 33010602011771号