楼上,我帮你改一个小地方,就是最后1句
printf("\nThere are %d %c in string '%s'.\n", cnt, ch, str);
另while(str[i++]) ,改成while(str[++i])是不是更好一点
#include
#define MAX 100
void main()
{
char str[MAX], ch;
int i=0, cnt=0;
printf("Input a string:");
gets(str);
printf("Input a char:");
ch = getchar();
while(str[i++])
if(str[i-1]==ch)
cnt++;
printf("\nThere are %d '%c's in string '%s'.\n", cnt, ch, str);
}