7-22 統計字符出現次數 (20分)
本題要求編寫程序,統計並輸出某給定字符在給定字符串中出現的次數。
輸入格式:
輸入第一行給出一個以回車結束的字符串(少於80個字符);第二行輸入一個字符。
輸出格式:
在一行中輸出給定字符在給定字符串中出現的次數。
輸入樣例:
programming is More fun!
m
輸出樣例:
2
#include<stdio.h>
#include<string.h>
int main()
{
int count=0;
char c;
char a[81];
gets(a);
int i;
scanf("%c",&c);
for(i=0;a[i]!='\0';i++)
{
if(a[i]==c)
count++;
}
printf("%d\n",count);
return 0;
}