计算字符个数

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


//解题思路:
//遍历每个字符,不区分字符的大小写需要明白相差32
#include <stdio.h>
#include <string.h>
int main()
{
	char str[10000];
	char ch;
	int len = 0;
	gets(str);            //以enter即'
'为字符串的结束符
	ch = getchar();       //getchar函数也是可行的
	//scanf("%c",&ch);    //使用scanf()函数输入字符也是可行的,
	for (int i = 0; i < strlen(str); i++)
	{
		if (str[i] == ch || str[i] == ch - 32 || str[i] == ch + 32)
			len++;
	}
	printf("%d
", len);

	return 0;
}

  

原文地址:https://www.cnblogs.com/277223178dudu/p/11336576.html