首页 > 试题广场 >

计算某字符出现次数

[编程题]计算某字符出现次数
  • 热度指数:1435710 时间限制: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
#include <iostream>
#include <string>

using namespace std;

int main(){
    string str;
    char c;
    cin>>str>>c;
    int count = 0;
    for(int i=0;i<str.length();i++){
        if(str[i]==c||str[i]==_toupper(c)||str[i]==_tolower(c)){
            count++;
        }
    }
    cout<<count;
    return 0;
}

发表于 2016-09-10 20:51:59 回复(0)
更多回答
推荐
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)
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string input;
    char s,m;
    getline(cin, input);
    cin>>s;
    m=s;
    if('A'<=s<='z')
    {
        if('A'<=s&&s<='Z')
            m=s+32;
        else if('z'>=s&&s>='a')
            m=s-32;
        int count=0;
        int length=input.size();
        for(int i=0;i<length;i++)
        {
            if(input[i]==s||input[i]==m)
            {
                count++;
            }
        }
        cout<<count<<endl;
        
    }
   
    return 0;
}


发表于 2021-02-14 21:59:09 回复(0)
function getWordCount(string,target){
    let map = {}
    for(char of string){
        map[char] ? map[char]++ : map[char] =1
    }
    if(target in map){
        return map[target]
    }
   
}

console.log(getWordCount('asdqdnqwdiuqnwdiuqwndiuqwdnqwd','d'))
JavaScript using hashtable
发表于 2020-02-04 15:55:39 回复(0)
var readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
var lines = [];
var countLine=1;
rl.on("line", function(line) {
    //将输入放到新建的空数组中
    lines.push(line);
    if(countLine==2){
    var str=lines[0].trim().toLowerCase();
    var c=lines[1].trim().toLowerCase();
    var count=0;
    var index=0;
    while(str.indexOf(c,index)!==-1&&index<str.length){
        index=str.indexOf(c,index)+1;
        count++;
        }
    countLine=1;
    lines=[];
    console.log(count);
    }
    else{
        countLine++;
    }
});



发表于 2019-07-03 10:00:55 回复(1)
#include<iostream>
#include<string>
#include<cmath>
using namespace std;

int PrintCharNum(string &str, char c)
{     int count = 0;     for (int i = 0; i<str.size(); i++)     {         if (abs(str[i] - c) == 0 || abs(str[i] - c) == 32)             count++;     }     return count;
}

int main()
{     string str;     char c;     getline(cin, str);     cin >> c;     cout << PrintCharNum(str, c) << endl;     return 0;
}
大小写字母相差32,可以用其差的绝对值来进行条件判定。
编辑于 2018-11-28 16:48:56 回复(0)
ASCII暴算,反正只有字母和数字不是?
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int sum;
int main()
{
    string a;
    char b;
    cin>>a>>b;
    int len=a.length();
    while(len--)
        if(a[len]==b||a[len]==b+32||a[len]==b-32)sum++; 
    cout<<sum;
    return 0;
}

发表于 2018-09-29 20:03:08 回复(0)
package com.gulamjan.t011.华为计算字符个数;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
 public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String string = br.readLine();
  String charString = br.readLine();
  char[] ch = charString.toCharArray();
  while ((string != null)) {
   System.out.println(getStatistics(string, ch));break;
  }
 }
 public static int getStatistics(String  string ,char ch[]) {
  char array[] = string.toCharArray();
  String chString = String.valueOf(ch);
  int counter = 0;
  for (int i = 0; i < array.length; i++) {
   String arr = String.valueOf(array[i]);
   if (arr.equalsIgnoreCase(chString)) {
    counter++;
   }
  }
  return counter;
 }
}
发表于 2017-10-04 10:11:28 回复(0)
五行代码搞定一切
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
 string words;
    char c;
    getline(cin,words);
    cin>>c;
    cout<<count(words.begin(),words.end(),toupper(c))+count(words.begin(),words.end(),tolower(c));
    return 0;
}

发表于 2017-06-25 12:40:32 回复(1)
int countOfStr(char *ptr,char key)
{
int count = 0;
int dis = 'a'-'A';
for(;*ptr != '\0';++ptr)
{
if(0 == abs(*ptr - key) || dis == abs(*ptr - key))
++count;
}
return count;
}
发表于 2017-06-08 22:51:34 回复(0)
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
 int i;
 int a = 0, b = 0, d = 0, e;
 char name[5000];
 char c;
 cin.get(name, 5000);
 cin >> c;
 for (i = 0; i<5000; i++)
 {
  if (c >= 'a'&&c <= 'z')
  {
   if (name[i] == c || name[i] == c - 32)
   {
    a++;
   }
   else
   {
    continue;
   }
  }
  else if (c >= 'A'&&c <= 'Z')
  {
   if (name[i] == c || name[i] == c + 32)
   {
    b++;
   }
   else
   {
    continue;
   }
  }
  else if (name[i] == c)
  {
   d++;
  }
  else
  {
   continue;
  }
 }
 e = a + b + d;
 cout << e;
} 

发表于 2016-12-06 12:53:32 回复(1)
//此算法可消除大小写
#include <iostream>
#include <map>
using namespace std;
int main(){
    map<char,int>words;
    char ch;
    while ((ch=getchar())) {
        if(ch=='\n')
            break;
        else if(ch>='a'&&ch<='z')
            {
            ch-=('a'-'A');
            words[ch]++;
        	}
        else    
            words[ch]++;
    }
    cin>>ch;
    if(ch>='a'&&ch<='z')
    {
        ch-=('a'-'A');
    }
    cout<<words[ch]<<endl;
    return 0;
}


发表于 2016-09-20 16:26:12 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        //char ch2= sc.nextLine().charAt(0);
        String str2= sc.next();
        //char[] ch = str.toCharArray();
        int sum = 0;
        for(int i = 0;i<str.length();i++){
            int result = str2.compareToIgnoreCase(String.valueOf(str.charAt(i)));
            if(result==0){
                sum++;
            }
        }
        System.out.println(sum);
    }
}

发表于 2016-06-14 15:33:21 回复(0)
用java在本地写的同学一定不能带上package啊。。。报错说数组越界。。。愁死人

发表于 2015-12-29 21:32:12 回复(0)
#include<iostream>
#include<string>

using namespace std;

int main()
{
    string s,cs;
    getline(cin,s);
    getline(cin,cs);
    char c = tolower(cs[0]);
    int count = 0;
    for(int i = s.size() - 1;i >= 0;--i)
    {
        if(tolower(s[i]) == c) ++count;
    }
    
    cout << count << endl;
    
    return 0;
}

发表于 2017-07-03 17:05:21 回复(0)
a=input().lower()
b=input().lower()
print(a.count(b))

python3 solution, easy to understand!


编辑于 2018-03-28 14:17:35 回复(36)
#include<stdio.h>
#include<string.h>
int main(){
    char str[1000];
    char c,a;
    int i=0,cnt=0;
    scanf("%s",str);
	getchar();
    scanf("%c",&c);
    if(c>='A'&&c<='Z'){
        a=c+32;
    }else if(c>='a'&&c<='z'){
        a=c-32;
    }
    for(i=0;i<strlen(str);i++){
        if(str[i]==c||str[i]==a)
            cnt++;
    }
    printf("%d\n",cnt);
    return 0;
}

发表于 2016-08-11 14:56:10 回复(3)
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s1 = "";
        String s2 = "";
        s1 = in.nextLine();
        s2 = in.nextLine();
        int count = 0;
        for(int i = 0; i < s1.length(); i++) {
            String s = s1.substring(i,i+s2.length());
            if(s2.equalsIgnoreCase(s)) {
                count++;
            }
        }
        System.out.println(count);
        
        
    }
}

发表于 2021-06-01 16:04:27 回复(1)
// 唯一的坑在于居然单独的单词是换行的,这里用replaceAll换掉所有匹配的字符
// 然后比较长度差就可以了
import java.util.*;
public class Main{
    public static void main(String ... args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()){
            String words = sc.nextLine();
            String word = sc.nextLine();
            int count = words.length() - words.replaceAll("(?i)" + word, "").length();
            System.out.println(count);
        }   
    }
}

发表于 2018-03-21 09:18:41 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s;
    char a;
    cin >> s >> a;
    int n = 0;
    char a2;  //如果a是字母,则a2是a对应的大小写反转的字母
    if(a>='a' && a<='z')  //小写字母比大写字母的ASCII码大32,且字母越靠后ASCII码越大
    {
        a2 = a - 32;
    }
    else if(a>='A' && a<='Z')
    {
        a2 = a + 32;
    }
    for(int i = 0;i <= s.size()-1;i++)
    {
        if(s[i] == a || s[i] == a2) n++; //不区分大小写的字符检测、计数
    }
    cout << n << endl;
    return 0;
}

发表于 2017-08-13 16:43:46 回复(0)
#include <iostream>
#include <string>

using namespace std;

int main() {
    string a;
    getline(cin,a); 
        int n=a.length(),m=0;
        char b;
        cin>>b;
        for (int i=0;i<n;i++) {
            if (toupper(a[i])==toupper(b)) ++m;
        }
        cout<<m<<endl;
    return 0;
}

发表于 2017-06-22 01:26:15 回复(0)
#include <iostream>
#include<string>

using namespace std;

int main()
{
    string str;
    char s;
    while(cin>>str>>s){
            int a[123]={0};
            for(int i=0;i<str.size();i++){
                a[str[i]]++;
            }
            int count=0;
            for(int i=0;i<123;i++){
                if(i==s||(i-32)==s||(i+32)==s)
                    count+=a[i];
            }
            cout<<count;
            cout<<endl;
    }
    return 0;
}

发表于 2016-09-06 16:47:25 回复(1)