题目: 编程实现函数fun的功能是:查找字符串str中值为x的元素,返回找到值为x的元素个数,并把这些值为x的下标依次保存在数组bb中。例如,在“abcdefahij”中查找‘a’,结果为:2个‘a’,下标依次为0、6。
#include<iostream>
#include<string.h>
#include<cmath>
using namespace std;
int fun (string str, char x, int* bb)
{
int sum=0,len = sizeof(str);
for (int i = 0; i < len; i++)
{
int s1=str[i], s2=x;
if (s1 == s2)
{
bb[sum]=i+1;
sum++;
}
}
return sum;
}
int main()
{
char x;
string str;
cin >> str>> x;
int* bb=new int[sizeof(str)];
bb=NULL;
cout << fun(str,x,bb)<<endl;
for(int i=0;i<fun(str,x,bb);i++)
cout<<bb[i]<<" ";
delete [ ]bb;
return 0;
}
程序可以运行但是为什么答案不对