copy and tolower

Hey all,

I have

char *tokens[buffer] where buffer is an int of some positive value.

The contents in the array will be some random information that has been read in from a previous getline.

What is the most efficient way to put the contents into something else and do a tolower to remove any caps.

Example:

tokens{23] contains the value "Capitol"

what it the quickest way to store "capitol" as a value I can use one time. It is ok to rewrite the variable each time as I will be looping thru the array. The tolower value does not have to be kept permanently.

Let me know if my question is not clear. Actual code is most helpful.
Is the question unclear? Is it in the wrong place?
1
2
3
string someString(tokens[23]);
std::transform(someString.begin(), someString.end(), someString.begin(),
               (int(*)(int)) std::tolower);
Sorry for the delay getting back to this.

The code given is giving errors

std::transform and std::tolower both cause errors

Error: namespace "std" has no memeber "transform" and
Error: namespace "std" has no member "tolower"

Did you include the right headers?
1
2
#include <algorithm>
#include <cctype> 
Thank you.

adding the headers corrected the issue and the conversion is working.

The item stored in the char * tokens is sucessfully converted to lower case and stored in a string.

I believe I have created another problem. The function that I have to send the info to wants a char * value like the original value. I cannot send the original value that is a char * because it could have capitol letters and the new value that is all lower case is a string.



I added strcpy to copy the string value back to the token location and all is good. Thank you both for your help.
I have a question
why do you cast ( int(*)( int )) std::tolower instead of character .
There are different versions of the tolower function and the cast is needed to pick the correct one.
thanks peter87
You should not be using a cast like that. Choose the proper tolower() function this way:

[edit] here's a link
http://www.cplusplus.com/forum/general/28440/#msg153216
Last edited on
Why not, Duoas? ptr_fun is deprecated (§D.8.2[depr.adaptors]), while overload resolution with cast expression is timeless (§13.4[over.over]/5)
Ah, I didn't know that...
Alas. Direct C-casts are ugly.
Topic archived. No new replies allowed.