Or you could just learn how to use
string from the STL :rolleyes:
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Perhaps something like this?
#include <iostream>
#include <cstring>
char *foo(char *original)
{
size_t len = strlen(original) + 1;
char *copy = new char[len];
for ( size_t i = 0; i < len; ++i )
{
copy[i] = original[i];
}
return copy;
}
int main(void)
{
char *text = foo("hello world");
std::cout << text << '\n';
delete[] text;
return 0;
}
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
The pointer is returned and the memory is deleted in main.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
STL is as simple as pie :eek:
#include <iostream.h>
#include <string.h>
using namespace std;
int main(void)
{
string wut="kido It is better to use C++ style strings kido";
int i = 0;
for(i = wut.find("kido", 0); i != string::npos; i = wut.find("kido", i))
{
wut.erase(i, 4); // erases kido
i++;
}
cout<<wut; //ammended string
cout<<"\n";
system("pause");
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
string class is not a part of STL...It is part of ANSI standard C++
Ya thats wat I meant. But the way the string class behaves it more or less could be part of the STL.
Anyway, learn about c++ style strings or get left behind.
:rolleyes:
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439