首页 > 试题广场 >

计算某字符出现次数

[编程题]计算某字符出现次数
  • 热度指数:1443174 时间限制: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,保证 c 为大小写字母或数字。


输出描述:
\hspace{15pt}在一行上输出一个整数,代表统计结果。
示例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)
    let arr=[]
    while(line = await readline()){ 
        arr.push(line)
    } 
    let r=arr[0].toLocaleLowerCase().split(arr[1].toLocaleLowerCase()).length-1
    console.log(r)

发表于 2025-11-05 23:57:12 回复(0)
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    let str = await readline()
    let char = await readline()
    let count = 0;

    for(let i = 0;i < str.length;i++ ){
        if(str.charAt(i).toLowerCase() == char.toLowerCase() ){
            count++;
        }
    }
    console.log(count)
}()

发表于 2024-10-02 22:09:56 回复(0)
const rl = require("readline").createInterface({
  input: process.stdin,
  output: process.stdout,
});

const num = 2;
let inputs = [];

rl.on("line", function (line) {
  inputs.push(line);
  if (num === inputs.length) {
    const [str, charStr] = inputs;
    if (str.length < 1 || str.length > 1000) return;
    const nums = str.match(new RegExp(charStr, 'ig'));
    console.log(nums ? nums.length : '0');
    inputs = [];
  }
});

写的难受死我了,各种输入输出,烦死了.
发表于 2024-09-26 23:31:27 回复(0)
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    // 输入两行 第一行输入字符串 第二行输入单个字符
    let line1 = await readline();
    let line2 = await readline();

    // 对第一行进行全部小写处理
    let tokens = line1.toString().toLowerCase();

    // 对第二行进行小写处理
    let r = line2.toString().toLowerCase();

    // 计数
    let count = 0;

    // 循环第一行的字符串与第二行输入的字符进行比对 若相同 计数加1
    for (let i = 0; i < tokens.length; i++) {
        if (tokens[i] == r) count++;
    }

    // 输出
    console.log(count);
})();

发表于 2024-08-21 10:28:22 回复(0)
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    let arr = [];
    while ((line = await readline())) {
        arr.push(line);
        if (arr.length == 2) {
            let line1 = arr[0];
            let line2 = arr[1];
            const regex = new RegExp(line2, "gi"); // 忽略大小写匹配
            const matches = line1.match(regex); // 使用 match() 方法查找所有匹配项
            arr = [] // 循环输入
            console.log(matches ? matches.length : 0);
        }
    }
})();
发表于 2024-05-27 16:41:51 回复(0)
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    // Write your code here
    let arr = [];
    while ((line = await readline())) {
        arr.push(line);
    }
    let template = arr[0];
    let target = arr[1];
    let tempArr = template.toUpperCase().split(target.toUpperCase());
    let counts = tempArr.length - 1;

    console.log(counts);
})();

编辑于 2024-04-25 10:36:53 回复(0)
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    // Write your code here
    while ((line = await readline())) {
        const text = line.slice(0, line.length - 1);
        const char = line.slice(line.length - 1);
        console.log(text.match(new RegExp(char, "igm")).length);
    }
})();
错哪里了?
发表于 2024-03-27 00:44:16 回复(1)

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    // Write your code here
    let a = await readline()
    let b = await readline()
    let str = a.toLowerCase()
    let key = b.toLowerCase()
    const reg = new RegExp("["+key+"]",'g')
    console.log((str.match(reg)||[]).length)
}()

发表于 2024-03-05 21:28:23 回复(0)
void async function () {
    // Write your code here
let a = await readline();
let str = a.toLowerCase();
let b = await readline();
let target = b.toLowerCase();
let count = 0;
for(let i = 0; i < str.length; i++) {
    if(str[i] == target) {
        count++;
    }
}
console.log(count);
}()
发表于 2023-08-15 17:16:29 回复(0)
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    // Write your code here
    let count = 0;
    let arr = [];
    while(line = await readline()){
        arr.push(line.toUpperCase())
    }
    let upper_a = arr[0]
    let test = arr[1]
    for(let i of upper_a)
        if(test === i) count++;
        
    console.log(count);
}()

发表于 2023-08-01 20:28:52 回复(0)
void async function () {
    // Write your code here
    while(line = await readline(), char = await readline()){
        // 有变量的正则
        const reg = new RegExp(char,'gi')
        const len = line.length
        // 非空格
       if (char.trim()) {
          line = line.replaceAll(reg, '')  
       }
       console.log(len - line.length)
    }
}()
发表于 2023-07-14 16:20:08 回复(0)
什么答题模板,醉了
发表于 2023-07-03 18:08:28 回复(0)
这个答题模版真的绝了
发表于 2023-06-05 10:46:03 回复(0)
void async function () {
    // Write your code here
    let result = []
    let count = 0
    while(line = await readline()){
        result.push(line)
        if( result.length == 2){
            result[0].toUpperCase().split("").forEach(el =>{
               el == line.toUpperCase()?count++:''
            })
            console.log(count)
        }
    }
   
 
}()

发表于 2023-05-22 20:22:08 回复(0)
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
const arr = [];
void (async function () {
  // Write your code here
  while ((line = await readline())) {
    let tokens = line.toString();
    arr.push(tokens.toLowerCase());
  }
  let count = 0;
  for (let k = 0; k < arr[0].length; k++) {
    if (arr[0][k] === arr[1]) {
      count++;
    }
  }
  console.log(count);
})();

发表于 2023-05-16 17:39:11 回复(0)
方法一
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    // Write your code here
    let arr = []
    while(line = await readline()){
        arr.push(line.toLowerCase())
    }
    let first = arr[0]
    let second = arr[1]
    let firstArr = first.split('')
    let newArr = firstArr.filter(item => item === second)
    console.log(newArr.length)
}()
方法二
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    // Write your code here
    let arr = []
    while(line = await readline()){
        arr.push(line.toLowerCase())
    }
    let first = arr[0]
    let second = arr[1]
    let newArr = first.split(second)
    console.log(newArr.length - 1)
}()



发表于 2023-03-14 11:00:03 回复(0)
const rl = require("readline").createInterface({ input: process.stdin });

let arr = [];

rl.on("line", function(line) {
    arr.push(line);
    let count = 0;
    
    if (arr.length == 2) {
        let [input, str] = arr;
        str = str.toLowerCase();
        let input_arr = input.split("");
        for (let i = 0; i < input_arr.length; i++) {
            if (input_arr[i].toLowerCase() == str) {
                count++;
            }
        }
        
        console.log(count);
    }
})

发表于 2022-12-04 04:28:07 回复(1)