#include <iostream>
#include <memory>
#include <cstring>
#include <string>
using namespace std;
std::unique_ptr<char[]> ToLower2(const char * const source)
{
int length = strlen(source) + 1;
char * dest = new char[length + 1];
for (int index = 0; index < length; ++index)
{
if (source[index] == ' ')
dest[index] = ' ';
else
dest[index] = tolower(source[index]);
}
return std::unique_ptr<char[]>(dest);
}
char * ToLower(const char * const source) // should use const really if you don't intend to change it
{
int length = strlen(source) + 1;
char * dest = new char[length + 1];
for (int index = 0; index < length; ++index)
{
if (source[index] == ' ')
dest[index] = ' ';
else
dest[index] = tolower(source[index]);
}
return dest;
}
int main() {
std::string s = "BLA BLA";
{
char * lower = ToLower(s.c_str());
cout<<lower<<endl; // use lower
//use some more....
//finished
delete [] lower;
}
{
unique_ptr<char[]> lower = ToLower2(s.c_str());
cout<<lower.get()<<endl; // use lower
//use some more....
//finished, lower will "delete itself"
}
return 0;
}