2022春招华为笔试题-(2)
题目:写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字符,然后输出输入字符串中该字符的出现次数。(不区分大小写字母)数据范围: 1000 \1≤n≤1000输入描述:第一行输入一个由字母和数字以及空格组成的字符串,第二行输入一个字符。输出描述:输出输入字符串中含有该字符的个数。(不区分大小写字母)思路:这是一种常见的字符串问题一般作为笔试最简单
Tsukiyo
January 26, 2022 2 min read 1820

The original body of this article is written in Chinese.
题目: 写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字符,然后输出输入字符串中该字符的出现次数。(不区分大小写字母)
数据范围: 1000 \1≤n≤1000 输入描述: 第一行输入一个由字母和数字以及空格组成的字符串,第二行输入一个字符。
输出描述: 输出输入字符串中含有该字符的个数。(不区分大小写字母) 思路:这是一种常见的字符串问题一般作为笔试最简单的题目 所以可以用不同方式去解 1、‘这是我见过最屌的大佬解答’
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
String input1= s.nextLine().toLowerCase();
String input2 = s.nextLine().toLowerCase();
String split=input1.replaceAll(input2,"");
int length=input1.length()-split.length();
System.out.println(length);
}
}
2、第二种方法我用的字符串套在循环里去比较
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str= sc.nextLine().toUpperCase();
char st = sc.nextLine().toUpperCase().charAt(0);
int count=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)==st){
count++;
}
}
System.out.println(count);
}
}
3、当然你第二个不用char而是用String也行,下面String类型进行比较就需要用equals
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str= sc.nextLine().toUpperCase();
String st = sc.nextLine().toUpperCase();
int count=0;
for(int i=0;i<str.length();i++){
if(st.equals(String.valueOf(str.charAt(i)))){
count++;
}
}
System.out.println(count);
}
}
If you enjoyed this piece, leave a like or a few words.
CSDN · 1 CSDN comments
Read originalYou may also like
All writing
Code Notes
Code Notes@Retryable 注解的使用与深度原理解析
它在 Spring 的 AOP(面向切面编程)机制的支持下,能够在方法执行失败时,根据指定的策略自动重试。注解时,Spring 会自动创建一个代理对象(proxy),并在调用目标方法时,通过拦截器(Interceptor)拦截方法的调用。,它将会自动重试,最多重试 3 次,每次重试之间的延迟时间为 2 秒,且每次重试的延迟时间会按 1.5 倍增长。:过于频繁

Leave a light on
00 repliesNo one has spoken yet. You can be the first light.