描述
写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字符,然后输出输入字符串中该字符的出现次数。(不区分大小写字母)
数据范围: 1 \le n \le 1000 \1≤n≤1000
输入描述:
第一行输入一个由字母和数字以及空格组成的字符串,第二行输入一个字符。
输出描述:
输出输入字符串中含有该字符的个数。(不区分大小写字母)
输入:
ABCabc
A
输出:
2
#include <stdio.h>
#include <string.h>
#define M_MAX_STR_LEN (1000 + 1)
int main()
{
unsigned cnt = 0;
char *pChar = NULL;
char inputChar = '\0';
unsigned int i, len = 0;
char strBuf[M_MAX_STR_LEN] = {0};
gets(strBuf);
// printf("strBuf: %s\n", strBuf);
scanf("%c", &inputChar);
// printf("inputChar: %c\n", inputChar);
// pChar = strBuf;
len = strlen(strBuf);
// printf("len: %u\n", len);
for (i = 0; i < len; i++) {
/* 给定的字母转换为小写字母 */
if (tolower(strBuf[i]) == tolower(inputChar)) {
cnt++;
}
}
printf("%u\n", cnt);
return 0;
}