【華爲機試練習】計算字符個數

題目描述
寫出一個程序,接受一個由字母和數字組成的字符串,和一個字符,而後輸出輸入字符串中含有該字符的個數。不區分大小寫。
輸入描述:
第一行輸入一個有字母和數字以及空格組成的字符串,第二行輸入一個字符。
輸出描述:
輸出輸入字符串中含有該字符的個數。ide


解法(C語言版):code

#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;
}
相關文章
相關標籤/搜索