I have already bulid the basic structure by using the loop + replace,In C++, the str.replace is only to replace single string, however, in some cases, we need to replace all the same string, My code can compile successfully and can output to the screen, but it seems that it does not replace successfully.
Thanks in advance
here's my code:
#include <iostream>
#include <fstream>
#include <string>
int main(void)
{
// initialize
std::ofstream fout;
std::ifstream fin;
fout.open("cad.dat");
fout << "C is a Computer Programming Language which is used worldwide, Everyone should learn how to use C" << std::endl;
fin.open("cad.dat");
std::string words;
getline(fin,words);
std::cout << words << std::endl;
while(1)
{
std::string::size_type pos(0);
if (pos = words.find("C") != std::string::npos && words[pos+1] != ' ') //the later one is used to detect the single word "C"
{
words.replace(pos, 1, "C++");
}
else
{
break;
}
}
std::cout << words;
}