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

计算某字符出现次数

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

#include <stdio.h>
int isABC(char buf){
if((buf >= 'A') && (buf <= 'Z')) return 1;
if((buf >= 'a') && (buf <= 'z')) return 2;
if((buf >= '0') && (buf <= '9')) return 3;
return 0;
}
int main() {
    char str[1024];
    char buf = ' ';
    char target = ' ';
    int end=0;
    int index = 0;
    int counter = 0;
    int type = 0;

    while(isABC(buf)) scanf("%c", &buf);

    str[0] = buf;

    while(1)
    {
        scanf("%c",&buf);
        if(isABC(buf)) {
            str[end] = buf;
            end++;
            }
        else if(buf==' '){
         str[end] = buf;
         end++;
        }
        else if(buf=='\n') break;
        else continue;
    }
    scanf("%c",&target);
    type = isABC(target);
    for(index = 0;index <= end;index++)
    {
        switch (type){
            case 1:if((str[index] == target)||(str[index] == target+32)){
                counter++;
                break;
                }
                else break;
            
            case 2:if((str[index] == target)||(str[index] == target-32)){
                counter++;
                break;
                }
                else break;
            case 3:if(str[index] == target) counter++;
                    else break;
            default :break;
        }
      
    }

    printf("%d",counter);    
    

    return 0;
}

全部评论
暴力解法
点赞 回复 分享
发布于 今天 00:36 广东

相关推荐

评论
点赞
收藏
分享

创作者周榜

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