计算字符个数
题目描述
写出一个程序,接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。
输入描述:
输入一个有字母和数字以及空格组成的字符串,和一个字符。
输出描述:
输出输入字符串中含有该字符的个数。
输入例子:
ABCDEF A输出例子:
1import java.util.Scanner; public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);while(in.hasNextLine()) {int num = 0;String strAll = in.nextLine();String strOne = in.nextLine();String s1 = strAll.toUpperCase(); // 转换成大写(注意String不可改变性质)String s2 = strOne.toUpperCase(); // 转换成大写 strAll.toUpperCase();strOne.toUpperCase();char[] chAll = s1.toCharArray();char[] chOne = s2.toCharArray();for(int i = 0; i < chAll.length; i ++) {if(chAll[i] == chOne[0]) {num ++;}}System.out.print(num);}} }
转载于:https://www.cnblogs.com/zywu/p/5802743.html
总结
- 上一篇: PageRank算法--从原理到实现
- 下一篇: spring security 允许 i