题解 | #统计字符串中字母出现次数#

统计字符串中字母出现次数

https://www.nowcoder.com/practice/83350872bdb5406fa706895d5efb1c55

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String string = "H e l l o ! n o w c o d e r";
        Scanner scanner= new Scanner(System.in);
        String word = scanner.next();
        scanner.close();
        System.out.println(check(string, word));
    }

    public static int check(String str, String word) {
                int count=0;
                word.toCharArray();
        //write your code here......
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)==word.charAt(0)){
                 count++;
            }
        }
        return count;
    }
}

全部评论
当然也可以遍历字符串用equls(word)直接进行字符串之间的比较都是可以的
点赞 回复 分享
发布于 2024-05-30 15:59 河南
主要思想就是遍历字符数组,但是请注意字符和字符串不可以直接进行比较,所以要先将字符串数组,转化为字符串组,这也是为啥要写word.toCharArray();so就是如此
点赞 回复 分享
发布于 2024-05-30 15:57 河南

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务