copy and tolower

Dec 6, 2011 at 2:31am
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.
Dec 6, 2011 at 1:14pm
Is the question unclear? Is it in the wrong place?
Dec 6, 2011 at 1:21pm
1
2
3
string someString(tokens[23]);
std::transform(someString.begin(), someString.end(), someString.begin(),
               (int(*)(int)) std::tolower);
Dec 13, 2011 at 4:17pm
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"

Dec 13, 2011 at 4:27pm
Did you include the right headers?
1
2
#include <algorithm>
#include <cctype> 
Dec 13, 2011 at 5:09pm
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.



Dec 13, 2011 at 5:27pm
I added strcpy to copy the string value back to the token location and all is good. Thank you both for your help.
Dec 13, 2011 at 5:30pm
I have a question
why do you cast ( int(*)( int )) std::tolower instead of character .
Dec 13, 2011 at 5:32pm
There are different versions of the tolower function and the cast is needed to pick the correct one.
Dec 13, 2011 at 6:02pm
thanks peter87
Dec 13, 2011 at 10:31pm
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 Dec 13, 2011 at 10:31pm
Dec 13, 2011 at 10:54pm
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)
Dec 14, 2011 at 8:53pm
Ah, I didn't know that...
Alas. Direct C-casts are ugly.
Topic archived. No new replies allowed.