题解 | #计算某字符出现次数#
计算某字符出现次数
https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1
本人新手,分享一种比较奇怪的思路
采用str.index(str2,int i) 即返回 str2在str的第i位及以后 出现的索引下标值。
采用for循环,每次返回计数一次并令i为当前的索引下标并且自加1,防止重复。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in1 =new Scanner(System.in); String s1=in1.nextLine(); String s2=in1.nextLine(); String t1=s1.toLowerCase(); String t2=s2.toLowerCase(); int t=0; for(int i=0;i<t1.length();i++){ if(t1.indexOf(t2,i)!=-1) { t++; i=t1.indexOf(t2,i); } } System.out.println(t); } }