第11次作业--字符串处理
# 题目一
编写一个应用程序,统计输入的一个字符串中相同字符的个数,并将统计结果输出。
# 源代码
##
import java.util.Scanner; /** * 计算每个字符的个数 * @author 喵 * @date 2019年11月19日下午7:38:30 */ public class CountChar { public static void main(String[] args) { Scanner input = new Scanner(System.in); String str = input.nextLine(); for (int i = 0; i < str.length(); i++) { //第一层取值 char ch = str.charAt(i); String str1 = String.valueOf(ch); int count = 0; if (str.indexOf(str1) > -1) { String str2 = str; for (int j = 0; j < str2.length(); j++) { //第二层拿值循环 int index = str2.indexOf(s1); str2 = str2.substring(index + 1); count ++; if (str2.indexOf(str1) == -1) { break; } } System.out.println(str1 + ":" + count); } } } }
## 测试结果

## 题目二
编写程序,输入一个字符串,判断该串中的字母能否组成一个回文串(回文串:一个字符串从前向后读取和从后向前读取都一样)。如:ab<c>c?ba
## 源代码
##
import java.util.Scanner; /** * 判断字符串是否是回文串 * @author 喵 * @date 2019年11月19日下午7:54:12 */ public class Huiwen { public static void main(String[] args) { Scanner input = new Scanner(System.in); String str1 = input.nextLine(); byte[] bytes = str1.getBytes(); for (int i = 0; i < bytes.length; i++) { if (bytes[i] == bytes[bytes.length -i-1]) { if (i == bytes.length - 1) { System.out.println("是回文"); } }else { System.out.println("不是回文"); break; } } } }
## 测试结果



浙公网安备 33010602011771号