Null-terminated string operation

Hi,

I'm making some null-terminated string operations, and for one of me operations
i need de replace a part of the string with another string. Does there exist a standard C++ function to replace a substring of a null-terminated string with another null-terminated string??

hannes
Use strstr to locate the substring you want to replace.
After that the replacing can be done by a few str(n)cpy calls.

http://cplusplus.com/reference/clibrary/cstring/

You may use the std::string member functions. Following is a sample code snippet that does find and replace on a null terminated C style string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

int main()
{
	const char* pcstr = "Hello, world!";
	string s1 = pcstr;
	string sold = "worl";
	string snew = "ki";
	string::size_type pos = s1.find(sold);
	if (pos != string::npos)
		s1.replace(pos, sold.size(), snew);
	cout << "Old - " << pcstr << endl; //Hello, World!
	cout << "New - " << s1 << endl; //Hello, Kid!
}
ok, i'll use the string class.

hannes
Topic archived. No new replies allowed.