首页 > 试题广场 >

计算某字符出现次数

[编程题]计算某字符出现次数
  • 热度指数:1394166 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的由大小写字母、数字和空格混合构成的字符串 s ,统计字符 c 在其中出现的次数。具体来说:
\hspace{23pt}\bullet\,c 为大写或者小写字母,统计其大小写形态出现的次数和;
\hspace{23pt}\bullet\,c 为数字,统计其出现的次数。
\hspace{15pt}保证字符 c 仅为大小写字母或数字。

输入描述:
\hspace{15pt}第一行输入一个长度 1 \leqq {\rm length}(s) \leqq 10^3 ,由大小写字母、数字和空格混合构成的字符串 s 。保证首尾不为空格。
\hspace{15pt}第二行输入一个字符 c ,代表需要统计的字符。


输出描述:
\hspace{15pt}在一行上输出一个整数,代表字符 c 在字符串 s 中出现的次数。
示例1

输入

HELLONowcoder123
o

输出

3

说明

\hspace{15pt}由于 o 为小写字母,因此统计其小写形态出现的次数和,即 3
示例2

输入

H E L L O Nowcoder123
1

输出

1
推荐
import java.util.*;
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner s=new Scanner(System.in);
        String all="";
        String one="";
        char[] ac;
        char temp;
        int num=0;
        while(s.hasNext())
        {
            //s.toUpperCase(),String 转化为大写
            //s.toLowerCase(),String 转化为小写
            //String字符转换,s.toCharArray()与s.charAt(index)
            //char字符转换,String.valueOf(c)转化为String
        	all=s.nextLine();
            one=s.nextLine();
            //存放原来所有的
            ac=all.toCharArray();
            //存放要的字符
            //temp=one.charAt(0);
            for(int i=0;i<ac.length;i++)
            {
            	if(one.equalsIgnoreCase(String.valueOf(ac[i])))    
                    num++;
            }
            System.out.println(num);
        }
        
    }
    
}

编辑于 2017-03-04 16:05:10 回复(45)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);//输入字符串
        String s = scanner.nextLine();//读取字符串 s

        char c = scanner.next().charAt(0);// 读取字符 c

        int count = countCharacter(s, c);
        System.out.println(count);
        scanner.close();
    }

    public static int countCharacter(String s,char c){
        int count = 0;
        //如果c是字母,统计其大小写形态
        if(Character.isLetter(c)){
            char lowerCase = Character.toLowerCase(c);
            char upperCase = Character.toUpperCase(c);
            for(int i = 0;i < s.length();i++){
                char currentChar = s.charAt(i);
                if(currentChar == lowerCase || currentChar == upperCase){
                    count++;
                }
            }

        }
        else if(Character.isDigit(c)){
            for(int i = 0; i < s.length();i++){
                if (s.charAt(i) == c){
                    count++;
                }
            }

        }
        return count;

    }
}

发表于 2025-03-01 10:58:04 回复(0)
import java.util.*;
import java.math.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            char[] firstStr = in.nextLine().toUpperCase().toCharArray();
            char secondStr = in.nextLine().toUpperCase().charAt(0);
            // String secondStr = in.nextLine().i
            int count= 0;
            int length = firstStr.length;
            for(int i =0;i<length;i++){
                if(secondStr ==firstStr[i]){
                    count++;
                }
            }
            System.out.println(count);
        }
    }
}
发表于 2025-02-11 20:53:05 回复(0)
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //读取字符串
        String s = in.nextLine();
        //读取字符
        char c = in.next().charAt(0);
        //计算出现的次数
        int count = countChar(s, c);
        System.out.println(count);
    }
    public static int countChar(String s, char c) {
        int count = 0;
        //遍历每一个字符
        for (int i = 0; i < s.length(); i++) {
            char current = s.charAt(i);
            //如果是字母,统计大小写
            if (Character.isLetter(c)) {
                if (Character.toLowerCase(current) == Character.toLowerCase(c)) {
                    count++;
                }
            }
            //如果是数字
            if (Character.isDigit(c)) {
                if (current == c) {
                    count++;
                }
            }
        }
        return count;

    }
}

发表于 2025-02-04 18:33:24 回复(0)
import java.util.Scanner;
import java.util.HashSet;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

        public static int strLength(String str,String strB){
         
            int count =0;
            String split[]=str.split("");
          for(int i=0;i<split.length;i++){
             if(strB.equalsIgnoreCase(split[i])){
                 count++;
              }
                     
          }

         return count;
        }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.next();
            String b = in.next();
            System.out.println(strLength(a,b));
        }

   
    }


}
发表于 2025-01-21 12:04:50 回复(0)
import java.util.Scanner;
//chatgpt
public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        //读取一个字符串
        String inputString = s.nextLine();
        //读取一个字符,取输入字符串第一位
        char targetChar = s.nextLine().charAt(0);
        //题目不要区分大小写,调用方法统一转为小写字符
        inputString = inputString.toLowerCase();
        targetChar = Character.toLowerCase(targetChar);
        //遍历输入字符串
        int count = 0;
        for(int i =0;i < inputString.length() ;i++){
            if(inputString.charAt(i) == targetChar){
                count++;
            }
        }

        System.out.println(count);
    }

}
发表于 2024-11-14 17:34:37 回复(0)
程序只需要一次读取输入的字符串和字符,然后遪行遍历字符串中的字符,进行比较和统计。
为什么计算两次循环的程序还比我快呢,大家知道为什么吗?
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String inputString = br.readLine().toLowerCase();
        char targetChar = Character.toLowerCase((char)br.read());
        int count = 0;
        for (char c : inputString.toCharArray()) {
            if (c == targetChar) {
                count++;
            }
        }
        System.out.println(count);
        br.close();
    }

}


发表于 2024-09-21 21:22:41 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        String idx = in.nextLine().toLowerCase();
        String lowerCaseInput = input.toLowerCase();
        int location = 0;
        int times = 0;
        while ((location = lowerCaseInput.indexOf(idx,location)) != -1){
            location ++;
            times ++ ;
        }
        System.out.println(times);
    }
}

发表于 2024-09-09 22:47:55 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String a = in.nextLine().toUpperCase();
        String b = in.nextLine().toUpperCase();
        String c = a.replaceAll(b,"");
        System.out.println(a.length()-c.length());
        in.close();
    }
    // public static void main(String[] args) {
    //     Scanner in = new Scanner(System.in);
    //     // 注意 hasNext 和 hasNextLine 的区别
    //     while (in.hasNext()) { // 注意 while 处理多个 case
    //         String a = in.nextLine().trim().toUpperCase();
    //         String b = in.next().toUpperCase();
    //         int c = 0;
    //         for (int i = 0; i < a.length(); i++) {
    //             if (b.charAt(0) == a.charAt(i)) {
    //                 c ++;
    //             }
    //         }
    //         System.out.println(c);
    //     }
    // }
}
发表于 2024-09-09 22:07:56 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String line = in.nextLine().toUpperCase();

        String charStr = in.nextLine().toUpperCase();
       
       
        line = line.replaceAll(charStr,charStr+"#");
        String[] array = line.split(charStr);
       
     
        System.out.println(array.length-1);
    }
}
发表于 2024-08-20 08:52:41 回复(0)
import java.util.Locale;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String str1 = scanner.nextLine();
            String str2 = scanner.nextLine();
            System.out.println(str1.length() - str1.toLowerCase().replace(str2.toLowerCase(), "").length());
        }
    }
}

发表于 2024-07-29 21:28:21 回复(0)
用时短的代码基本都是在测试数据的输入方式上下功夫,这样的排行合适吗?
发表于 2024-07-17 23:56:41 回复(0)
为什么我的hashmap无法赋值啊
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        
        // 注意 hasNext 和 hasNextLine 的区别
        String str=in.nextLine();
        String ch=in.nextLine();
        str=str.toLowerCase();
        ch=ch.toLowerCase();
        char[] ch1=str.toCharArray();
        char ch2=ch.charAt(0);
        HashMap<Character,Integer> cont=new HashMap<>();
        cont.replace(ch2,0);
        for(int i=0;i<ch1.length;i++)
        {
            if(ch1[i]==ch2)
            {
                int j=0;
                j=cont.get(ch2);
                cont.replace(ch2,j);
            }
        }
        System.out.println(cont.get(ch2));
    }
    }


发表于 2024-07-10 20:20:46 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.nextLine().toLowerCase();
            String b = in.nextLine().toLowerCase();
            System.out.println(a.length() - a.replaceAll(b,"").length());
        }
    }
}

发表于 2024-07-05 11:12:42 回复(0)
计算某字符在字符串中出现次数,Java实现,时间复杂度O(n)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String str = scanner.nextLine();
            char ch = scanner.nextLine().charAt(0);
            System.out.println(charCount(str, ch));

        }
    }

    public static int charCount(String str, char ch) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (Character.toLowerCase(str.charAt(i)) == Character.toLowerCase(ch)) {
                count++;
            }
        }
        return count;
    }
}


发表于 2024-06-27 22:06:43 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine().toLowerCase();
            char ch = in.nextLine().toLowerCase().charAt(0);
            System.out.println(test(str, ch));
        }
    }

    private static int test(String str, char ch){
        int count = 0;
        for(char c : str.toCharArray()){
            if(c==ch)
                count++;
        }

        return count;
    }
}

发表于 2024-06-01 22:12:37 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String line = in.nextLine();
        String matchStr = in.nextLine();
        char matchCharLowerCase = matchStr.toLowerCase().toCharArray()[0];
        char matchCharUpperCase = matchStr.toUpperCase().toCharArray()[0];
        char[] chars =  line.toCharArray();
        int result = 0;
        for(int i=0;i<chars.length;i++ ){
            if( (chars[i] == matchCharLowerCase) ||
             (chars[i] == matchCharUpperCase)){
                result ++;
            }
        }
        System.out.println(result);
    }
}

编辑于 2024-03-27 16:36:35 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String a = in.nextLine();
        String b =in.next();

        int sum=0;
        for(int i=0;i<a.length();i++){
           String temp = ""+a.charAt(i);
           if(temp.equalsIgnoreCase(b)){
            sum=sum+1;
           }
        }
        System.out.println(sum);

}
}


编辑于 2024-03-19 16:35:34 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case

            String s = in.nextLine().toLowerCase();
            String b = in.nextLine().toLowerCase();//一定要加toLowerCase(),否则会出现字符为空客的情况,结果为0;
            //s=s.toLowerCase();

            System.out.println(s.length()-s.replaceAll(b,"").length());
        }
    }
}
import java.util.Scanner;


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case

            String s = in.nextLine().toLowerCase();
            String b = in.nextLine().toLowerCase();//一定要加toLowerCase(),否则会出现字符为空客的情况,结果为0;
            //s=s.toLowerCase();

            System.out.println(s.length()-s.replaceAll(b,"").length());
        }
    }
}

编辑于 2024-03-18 00:17:31 回复(0)