列表

详情


JAVA30. 判断字符串中字母出现次数

描述

给定一个字符串,随机输入一个字母,判断该字母在这个字符串中出现的次数

输入描述

任意一个字母

输出描述

字母在字符串中出现次数

示例1

输入:

o

输出:

3

示例2

输入:

a

输出:

0

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Java 解法, 执行用时: 12ms, 内存消耗: 9588KB, 提交时间: 2022-06-21

import java.util.Scanner;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException{
        String string = "H e l l o ! n o w c o d e r";
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String word = br.readLine();
        System.out.println(check(string, word));
    }

    public static int check(String str, String word) {

        //write your code here......
        char a[] = str.toCharArray();
        int count = 0;
        for(int i=0;i<a.length;i++){
            if(word.charAt(0) == a[i]) count++;
        }
        return count;
    }
}

Java 解法, 执行用时: 14ms, 内存消耗: 9464KB, 提交时间: 2022-07-07

import java.util.Scanner;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException{
        String string = "H e l l o ! n o w c o d e r";
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String word = br.readLine();
        System.out.println(check(string, word));
    }

    public static int check(String str, String word) {

        char a[] = str.toCharArray();
        int count = 0;
        for(int i=0;i<a.length;i++){
            if(word.charAt(0) == a[i]) count++;
        }
        return count;
    }
}

Java 解法, 执行用时: 23ms, 内存消耗: 10512KB, 提交时间: 2022-02-08

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) {

        //write your code here......
        char c =word.charAt(0);
        int res=0;
        for(int i=0;i<str.length();i++)
            if(c==str.charAt(i))
                res++;
            return res;
    }
}

Java 解法, 执行用时: 25ms, 内存消耗: 10572KB, 提交时间: 2021-11-16

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) {

        //write your code here......
        int count=0;
        char[] arr=str.toCharArray();
        char w=word.toCharArray()[0];
        for(int i=0;i<arr.length;i++){
        if(w==arr[i]){
         count++;
        }
        }
        return count;
    }
}

Java 解法, 执行用时: 26ms, 内存消耗: 10456KB, 提交时间: 2021-11-23

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;//用来记录出现的次数
        for(int i =0;i<str.length();i++){
            //if(str.charAt(i) == ' '){continue;}
            if(str.charAt(i) == word.charAt(0)){
                count++;
            }
        }
        return count;

    }
}

上一题