Advertisement

Converting a string to upper

Started by September 17, 2000 08:37 PM
5 comments, last by gimp 24 years, 3 months ago
Is there a simple way to convert a (basic)string to upper that doesn''t require me writing a for loop or copying the whole string out to a char*? thanks gimp
Chris Brodie
#include

char *strupr(char *string);
Advertisement
it's crazy ... finding a single standard algorithm that lets you use a function to replace elements in a sequence was a bitch. I will mention the notable functions that do parts or related things, and last the solution (i think

the first and best hopeful was
for_each()
but it is not allowed to change the element

the second was
replace()
but it simple replace specific values with other specific values

the third was
transform()
this function didn't seem right because it took too many iterators .. but it IS correct ... the correct solution is something like this.

        #include <string>  // for string type#include <algorithm>  // for transform#include <cctype>  // for toupper functionusing namespace std;  // to keep it readablestring myString = "MixedCase";transform(myString.begin(),myString.end(),myString.begin(),toupper);    


in this form, transform takes four parameters, the first two are the start and end of the sequence to transform. The THIRD .. and key one is the start of the sequence to output too .. the key being the use of the same parameter for 1 and 3 so the input and output is the same (hence modifying the actual string) ... then the fourth is the function, which must be a unary function taking the same type and the container and return that type as well. The function is NOT allowed to change the element itself ... it must return the changed element ...

good luck

Edited by - Xai on September 17, 2000 10:12:09 PM

Edited by - Xai on September 17, 2000 10:13:16 PM
Isn''t strupr Windows only? Use toupper for chars. You could make a function that turns each array index to toupper.



-=[ Lucas ]=-
-=[ Lucas ]=-
Thanks.. that looks much better than my temp hack. I was pulling each element out, plonking it in a char[2] that has a null at the end, running strupr on the char2 then pushing char[0] back in to the string... bwwwweeerrrrr

I was even considering doing a literal ascii conversion (ie add like 34 or whatever to each element between the upper\lower bound of the lower case char on the ascii lookup table...

thanks

gimp

Chris Brodie
Looking at STRING.H, it appears that _strupr is part of standard C but strupr is not.

Tim
Advertisement
strupr is not a standard function. The best (portable) way to convert a string to uppercase is to loop through all the characters in the string and use the toupper function on each character.

This topic is closed to new replies.

Advertisement