连接:https://www.nowcoder.com/questionTerminal/a35ce98431874e3a820dbe4b2d0508b1
来源:牛客网
java
写出一个程序,接受一个有字母和数字以及空格组成的字符串,和一个字符,而后输出输入字符串中含有该字符的个数。不区分大小写。spa
输入描述:code
输入一个有字母和数字以及空格组成的字符串,和一个字符。
输出描述:字符串
输出输入字符串中含有该字符的个数。
示例1get
ABCDEF A
1
解题思路:逐个进行不区分大小写的比较,同时统计相等的次数io
package BiShiTi; import java.util.Scanner; public class m_0013 { public static void main(String[] args) { @SuppressWarnings("resource") Scanner scan = new Scanner(System.in); String targetStr = scan.nextLine(); String targetChar = scan.nextLine(); int counter = countTargetChar(targetStr, targetChar); System.out.println(counter); } static int countTargetChar(String targetStr, String targetChar ){ int targerStrLen = targetStr.length(); int counter = 0; char t; for(int i = 0; i < targerStrLen; i++){ t = targetStr.charAt(i); if (targetChar.toUpperCase().equals(String.valueOf(t)) || targetChar.toLowerCase().equals(String.valueOf(t))) { counter ++; } } return counter; } }