描述
写出一个程序,接受一个由字母、数字和空格组成的字符串,和一个字母,然后输出输入字符串中该字母的出现次数。不区分大小写,字符串长度小于500。
输入描述:
第一行输入一个由字母和数字以及空格组成的字符串,第二行输入一个字母。
输出描述:
输出输入字符串中含有该字符的个数。
示例1
输入:
ABCabc A
复制输出:
2
#include <stdio.h>
#include <string.h>
int main (){
char str[500];
char element;
gets(str);
scanf("%c", &element);
int total = 0;
int len = strlen(str);
for(int i = 0; i < len; i ++){
// 大小写的切换用 ascii码 差值 a、A = 32
if(element == str[i] || element == str[i] + 32 || element == str[i] -32){
total ++;
}else{
continue;
}
}
printf("%d", total);
return 0;
}