how can i copy a string to char

hi guys, i have a task needs to do some comparison for a string, but before compare them, it should be change to lower case. so here is the function for tolower;

1
2
3
4
5
6
7
8
string tolower(string str)
{
   char *strings;
   strings = (char*)str.c_str();
   for(int i = 0; i < strlen(strings); i++)
      strings[i] = tolower(strings[i]);
   return strings;
}


after comparison, i need to print out the original string. however, once i use this method, the string is changed cus the pointer of char point to that string.

so how can i change to copy a string to char without pointer?
You can construct an std::string with a const char*:
1
2
const char* msg = "hello world!";
std::string string(msg);


However, why do you bother converting to a char* at all? You can do this:
1
2
std::string str = "Hello World!";
str[2] = 'f'; // changes the third character of the message to 'f' 


This line in particular is bad:
strings = (char*)str.c_str();
You are removing the constness of a variable. You should avoid this wherever possible.

Regards
-Xander314
thanks mate, i'll have a try
Topic archived. No new replies allowed.