程序设计思路如下:
从键盘分别输入字符串和要统计的字符,然后对此字符串从头开始逐个与所统计的字符比较,如相同,则让计数器加1,知道字符串整体比较结束为止,计数器中就是需统计的字符的个数。
#include "
stdio.h"
main()
{ char str[100],ch; /*定义str字符串,定义ch用来存放要统计的字符*/
int cnt=0,i; /*定义cnt用作计数器,初始值为0*/
/*输入字符串*/
printf("请输入字符串:");
gets(str);
/*输入要统计的字符*/
printf("请输入要统计的字符:");
scanf("%c",&ch);
/*对此字符串从头开始逐个与所统计的字符比较,如相同,则让计数器加1,知道字符串整体比较结束为止*/
for( i=0;str[i];i++ )
if( str[i]==ch )
cnt++;
/*输出结果*/
printf("%s串中%c字符的个数是:%d个",str,ch,cnt);
}