首页 > 试题广场 >

统计字符

[编程题]统计字符
  • 热度指数:18880 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    统计一个给定字符串中指定的字符出现的次数。

输入描述:
    测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串。注意这里的字符串包含空格,即空格也可能是要求被统计的字符之一。当读到'#'时输入结束,相应的结果不要输出。


输出描述:
    对每个测试用例,统计第1行中字符串的每个字符在第2行字符串中出现的次数,按如下格式输出:
    c0 n0
    c1 n1
    c2 n2
    ... 
    其中ci是第1行中第i个字符,ni是ci出现的次数。
示例1

输入

I
THIS IS A TEST
i ng
this is a long test string
#

输出

I 2
i 3
  5
n 2
g 2
这题真是坑,题目中说会有#号指示结束,但是并没有,需要自己设计方法。在循环中,当本轮检测到的第一行和上次的第一行完全相同,说明已经结束了。
#include<stdio.h>
#include<string.h>
int main(void){
    char a[100];
    char b[100];
    char * c;
    int tongji[100];
    int flag=0,temp,k=0,l=0,jl;
    while(flag!=1){
        gets(a);
        if(l!=0){
            temp=0;
            for(int i=0;i<jl;i++){
                if(c[i]==a[i]){
                    temp++;
                }
            }
            if(temp==jl){
                break;
            }
        }
        
        
        l++;
        if(a[0]=='#'){
            flag=1;
            break;
        }
        gets(b);
        int len1=strlen(a);
        jl=len1;
        int len2=strlen(b);
        for(int i=0;i<len1;i++){
            temp=0;
            for(int j=0;j<len2;j++){
                if(b[j]==a[i]){
                    temp+=1;
                }
            }
            tongji[i]=temp;
        }
        for(int i=0;i<len1;i++){
            printf("%c %d\n",a[i],tongji[i]);
        }
        
        c=a;
    }

    return 0;
}


发表于 2022-03-23 15:31:22 回复(0)
  • 暴力法
#include <stdio.h>
#include <string.h>

char s[10];
char str[90];

int main()
{
    while(fgets(s, 10, stdin) != NULL)
    {
        if (!strcmp(s, "#\n"))
            break;
        fgets(str, 90, stdin);
        for (size_t i = 0; i < strlen(s) - 1; i++)
        {
            int cnt = 0;
            for (size_t j = 0; j < strlen(str) - 1; j++)
            {
                if (s[i] == str[j])
                    cnt++;
            }
            printf("%c %d\n", s[i], cnt);
        }
    }
    return 0;
}
  • 建立字符和出现个数之间的映射
#include <stdio.h>                                                                                                                                            
#include <string.h>
 
char s[10];
char str[90];
int hash[128];
 
int main()
{
    while(fgets(s, 10, stdin) != NULL)
    {   
        if (!strcmp(s, "#\n"))
            break;
        fgets(str, 90, stdin);
        for (size_t i = 0; i < strlen(str) - 1; i++)
        {
            hash[(int)str[i]] ++; 
        }
        for (size_t i = 0; i < strlen(s) - 1; i++)
            printf("%c %d\n", s[i], hash[(int)s[i]]);
        memset(hash, 0, sizeof(hash));
    }    
    return 0;
}


发表于 2022-03-04 23:28:29 回复(0)
#include<stdio.h>
#include<stdlib.h>
int main(){
    char b[80];
    char a[80];
    char e;
    while(gets(a)!=NULL){
        gets(b);
        for(int i=0;a[i]!='\0';++i)
        {   int number=0;
             if(a[i]=='#')
                 break;
            for(int j=0;b[j]!='\0';++j)
                if (a[i]==b[j])
                      number++;
            printf("%c %d\n",a[i],number);}
    }
  return 0;
}

发表于 2022-01-31 11:17:13 回复(0)