【华为机试练习】计算字符个数

题目描述
写出一个程序,接受一个由字母和数字组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。
输入描述:
第一行输入一个有字母和数字以及空格组成的字符串,第二行输入一个字符。
输出描述:
输出输入字符串中含有该字符的个数。


解法(C语言版):

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

int main()
{
    char str[10000];
    char ch;
    int n, i, cnt;
    gets(str);
    ch = getchar();
    cnt = 0;
    if(strlen(str) != 0)
        for(i = 0; str[i] != '\0'; ++i)
            if((str[i] == ch) || (abs(str[i] - ch) == 32))
                cnt++;
    printf("%d\n", cnt);
    return 0;
}

猜你喜欢

转载自blog.51cto.com/13614527/2467166
今日推荐