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

计算某字符出现次数

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

#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#define N 1002
int main() {
    char str[N], c;
    int num, i, a;
    char * p;
    fgets(str, sizeof(str), stdin);
    c = getchar();
    p = str;
    num = 0;
    if (isalpha(c)) {
    if( 'a' <= c && c <= 'z')
    {
        a = -32;
    }
    else {
        a = 32;
    }
    for(i =0; i < N;i++)
    {   
        if (* p == c || * p == c + a ) {
            num++;
        }
         if (* p == '\0') {
                break;
            }
            p++;
    }
    }
    else {
         for(i =0; i < N;i++)
    {   
        if (* p == c ) {
            num++;
        }
         if (* p == '\0') {
                break;
            }
            p++;
    }
    }
    printf("%d", num);

    return 0;
}

全部评论

相关推荐

神哥了不得:放平心态,再找找看吧,主要现在计算机也变卷了,然后就比较看学历了,之前高中毕业你技术强,都能找到工作的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务