【题目】
【答案】
#include <iostream>
#include <vector>
using namespace std;
char* func(char* str1, char* str2)
{
vector<int> temp = {};
for (int i = 0; i < strlen(str1) ; i++)
{
for (int j = 0; j < strlen(str2); j++)
{
if (str1[i] == str2[j])
{
temp.push_back(i);
}
}
}
for (int i = 0; i < temp.size(); i++)
{
cout << temp[i] << endl;
str1[i] = str1[temp[i]];
}
str1[temp.size()] = '\0';
return str1;
}
int main()
{
char str1[] = "helloworld";
char str2[] = "hled";
func(str1, str2);
cout << str1 << endl;
system("pause");
return 0;
}
【疑问】
但是如果str1和str2定义成字符串常量指针: