题解 | #计算某字符出现次数#

计算某字符出现次数

https://www.nowcoder.com/practice/a35ce98431874e3a820dbe4b2d0508b1

#include <stdio.h>
#include <string.h>

int main() {
    char str[1000];
    char a;
    scanf("%[^\n]\n%c",str,&a);
    int len =strlen(str);

    if(a>='a'&&a<='z'){
        a = a-32;
    }
    int i=0;
    while(i<len){
        if(str[i]>='a'&&str[i]<='z'){
          str[i]=str[i]-32;
        }
        i++;
    }
    i=0;
    int count=0;
    while(i<len){
        if(str[i]==a){
            count++;
        }
        i++;
    }
    printf("%d",count);
    return 0;
}

全部评论
我一开始直接判断if(s[i]==c||s[i]==c-32||s[i]==c+32)就不对,没有通过第12/13个测试字符串,因为‘Q’=81,‘1’=49,把Q也算进去了
点赞 回复 分享
发布于 2023-05-20 16:50 四川

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务