You can see the fullcode below :
#include "iostream"
#include "string"
using namespace std;
string replaceAll(string text,string from,string to) {
string result = "";
int i = 0;
while(i < text.size())
{
if(i+from.size() <= text.size() && text.substr(i,from.size())==from )
{
result += to;
i += from.size();
}
else
{
result.push_back(text[i]);
i++;
}
}
return result;
}
int main() {
string a = "C++ is easy";
cout << a << endl;
a = replaceAll(a,"C++ is","C++ and Java are");
cout << a << endl;
return 0;
}
Sorry be4, I'm a new bie in java
ReplyDeleteIsn't there any build-in function in java to do the same job as yours?
Yes, you can use replaceAll() method in String class.
ReplyDelete