编写函数int count(char s[],char letter),其功能是统计字符letter在字符串s中出现的次数并作为函数值返回

2024-11-07 14:39:27
推荐回答(2个)
回答1:

解释:
#include
int count(char s[],char letter)
{ int i=0,sum=0;
while(s[i]!='\0')
{ if(s[i]==letter)
sum++;
i++;
}
return sum;
}
main()
{ char s[100], letter;
int i, sum;
printf("请输入一个字符串:\n");
gets(s);
printf("请输入一个字符:\n");
scanf("%c",&letter);
sum=count(s,letter);
printf("%c字符在字符串出现的次数是%d\n",letter,sum);
}

回答2:

#include
using namespace std;
int count(char s[],char letter)
{
int i=0,sum=0;
while(s[i]!='\0')
{
if(s[i]==letter)
sum++;
i++;
}
return sum;
}
int main()
{
char s[100],c;
cout<<"输入字符串"< cin>>s;
cout<<"输入字符"< cin>>c;
cout< return 0;
}