题解 | #计算某字符出现次数#

计算某字符出现次数

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

全部评论

相关推荐

01-14 19:01
吉首大学 Java
SpadeKTLSG:我是明白我为什么会看到这篇帖子了,你是我之前一起实习的好兄弟的吉首的学弟
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务