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

计算某字符出现次数

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

#include <stdio.h>
#include<stdlib.h>

//预留空间给'\0'和'\n'
#define SIZE 1002

int main() {
    char *buf = (char *)malloc(SIZE * sizeof(char));
    char c;
    int count;
    count = 0;
    fgets(buf, SIZE * sizeof(char), stdin);
    scanf("%c", &c);
    while(*buf != '\0')
    {

        if(c == *buf)
        {
            count++;
        }

        if((c >= 'A') && (c <= 'Z'))
        {
            if((c += 32) == *buf)
            {
                count++;
            }
        }else if((c >= 'a') && (c <= 'z'))
        {
            if((c -= 32) == *buf)
            {
                count++;
            }
        }
        buf++;
    }

    printf("%d", count);

    return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务