【函数】自定义函数统计字符个数

来源:互联网 发布:怎样注册淘宝号 编辑:程序博客网 时间:2024/06/06 08:45
#include<stdio.h>#include<string.h>int fun(char str[],char ch);int main(){int t;char str[100],ch;scanf("%d",&t);getchar();for(;t>0;t--){gets(str);ch=getchar();getchar();printf("%d\n",fun(str,ch));}}int fun(char str[],char ch){int i,l,count=0;l=strlen(str);for(i=0;i<l;i++){if(str[i]==ch){count++;}}return count;}


 

Problem B: 【期末考试-函数】统计字符个数

Description

编写函数:int fun(char str[ ], char ch),其功能是:求出str字符串中指定字符ch的个数,要求在主函数中输入字符串及指定字符,最后输出结果。

注意:必须编写函数fun来统计字符的个数,否则不能得分。

Input

第一行是测试数据的组数T,后跟T组测试数据。每组测试数据单独占两行,第一行是一个字符串str,字符串长度不超过100;第二行是要查找的字符ch。

Output

对于每组测试数据,输出结果占一行,为查找到的字符的个数。

Sample Input

2aaabb bccc 123b123 123 12 33

Sample Output

33

 

0 0
原创粉丝点击