我无法理解 char* 数组,无法替换一个 char* 数组的一部分,并在找到匹配段时插入不同的 char* 数组。这是我目前正在使用的代码,我不明白为什么我在 for 循环中的 if 检查不认为它是匹配的。
#include <iostream>
int main()
{
char* sampleString = "this is a test";
const char* oldString = "a test";
const char* newString = "now changed";
std::cout<< " String before amendment: " << std::endl;
std::cout<< sampleString << std::endl;
for (char* i = sampleString; *i; ++i)
{
std::cout<< i << std::endl;
std::cout<< oldString << std::endl;
if (i == old_str)
{
std::cout<<"found match"<< std::endl;
// replace oldString with newString
}
}
std::cout<< " String after amendment: " << std::endl;
std::cout<< sampleString << std::endl;
std::cin.get();
return 0;
}