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

计算某字符出现次数

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

#include <stdio.h>
#include<ctype.h>
//这里主要就是大小写字符处理比较一下
int main() {
    char str[1001]={0};
    char word;
    int count_alph=0;
    gets(str);
    scanf("%c",&word);
    //printf("word is %c\r\n", word);
    
    //遍历作比较
    for(int i=0; i< strlen(str);i++)
    {
        //字符串,数字
        if(toupper(str[i]) == toupper(word))
        {
            count_alph++;
        }
    }

    //空格,这里不知道怎么判定输入字符为空格的情况,特殊处理一下
    if(count_alph == 0)
    {
        for(int i=0; i< strlen(str);i++)
        {
            if(str[i] == ' ')
            {
                count_alph ++;
            }
        }
    }
    printf("%d", count_alph);
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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