8、C++编程实现函数fun的功能是:查找字符串str中值为x的元素,返回找到值为x的元素个数,并把这些值为x的下标依次保存在数组bb中。
时间: 2024-06-14 21:04:35 浏览: 113
以下是C++编程实现函数fun的功能的示例代码:
```cpp
#include <iostream>
using namespace std;
int fun(char* str, char x, int* bb) {
int count = 0;
int index = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == x) {
count++;
bb[index] = i;
index++;
}
}
return count;
}
int main() {
char str[] = "Hello World";
char x = 'o';
int bb[100];
int count = fun(str, x, bb);
cout << "Number of elements with value " << x << ": " << count << endl;
cout << "Indices of elements with value " << x << ": ";
for (int i = 0; i < count; i++) {
cout << bb[i] << " ";
}
cout << endl;
return 0;
}
```
输出结果为:
```
Number of elements with value o: 2
Indices of elements with value o: 4 7
```
阅读全文
相关推荐


















