求助一道题
  • 板块学术版
  • 楼主Hellen
  • 当前回复6
  • 已保存回复6
  • 发布时间2022/3/2 11:17
  • 上次更新2023/10/28 07:27:17
查看原帖
求助一道题
14315
Hellen楼主2022/3/2 11:17

题目: 编程实现函数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;
}

程序可以运行但是为什么答案不对

2022/3/2 11:17
加载中...