计算字符个数

题目描述

写出一个程序,接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。

输入描述:

输入一个有字母和数字以及空格组成的字符串,和一个字符。

输出描述:

输出输入字符串中含有该字符的个数。

输入例子:
ABCDEF
A
输出例子:
1

import 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);
        }
    }
}

 



posted @ 2016-08-24 14:12  no_one  阅读(200)  评论(0编辑  收藏  举报