Advertisement

lowercase(string)?

Started by May 26, 2002 12:04 PM
11 comments, last by Ragadast 22 years, 7 months ago
Know any way to lower case a string?
STL:

      template <class C>void to_lower(C *str){    while (*str)    {        *str = std::tolower(*str);        ++str;    }}      


MSVC:
_strlwr(str);

Win32:
CharLowerBuff(str, lstrlen(str));

[edited by - IndirectX on May 26, 2002 1:17:37 PM]
---visit #directxdev on afternet <- not just for directx, despite the name
Advertisement
tolower(string) and you need ctype.h or you could subtract 32 from each letter you want to make lower case.

"The most likely way for the world to be destroyed, most experts agree, is by accident. That''s where we come in; we''re computer professionals. We cause accidents."
-Nathaniel Borenstein
-----------------------------When men speak of the future, the Gods laugh.An apology for the devil: it must be remembered that we have heard one side of the case. God has written all the books.Samuel Butler (1835 - 1902)

  #include <string.h>void lowercase(char *string){     int len = strlen(string);     for(int i=0; i<len; i++)     {          if(string[i] >= ''A'' && string[i] <= ''Z'')               string[i] += 32;     }}  



- f l u c k y p o o
- the geek inside
- f l u c k y p o o
Thanks for the answers, but I tried them all and they return mismatch errors
a C string

      #include <ctype>#include <algorithm>using namespace std;char* str;size_t len = strlen( str );transform( str, str + len, str, tolower );            


a C++ string

        #include <ctype>#include <algorithm>#include <string>using namespace std;string str;transform( str.begin(), str.end(), str.begin(), tolower );      



Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on May 26, 2002 3:42:04 PM]

[edited by - Fruny on May 26, 2002 4:00:42 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
char *lowercase(char *string){  char *temp = string;    do  {    if(*temp < 91 && *temp > 64)    {      *temp += 32;    }  } while(*(++temp));  return string;} 


There's a cute lil' C approach that works.

Peace,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

EDITED FOR FORMATTING.

[edited by - zealouselixir on May 26, 2002 4:30:21 PM]

[twitter]warrenm[/twitter]

Results for methods so far submitted (test involves repeated conversion from upper to lower case within a loop):

IndirectX – 341 milliseconds
MSVC – 140 milliseconds
Win32 – 511 milliseconds
flucknugget – 200 milliseconds
Fruny method #1 – 330 milliseconds
Fruny method #2 – 1833 milliseconds (with slight modification)
zealouselixir – 180 milliseconds

Run your own tests if you think something''s wrong; just don''t flame me for it.

Peace,
ZE.


//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

[twitter]warrenm[/twitter]

ZealousElixir: I tried calling lowercase(string) but It sent back an error saying cannot convert string to char *.
quote: Original post by Ragadast
ZealousElixir: I tried calling lowercase(string) but It sent back an error saying cannot convert string to char *.


You can't feed a constant literal, ie "SoMe StRiNg", to lowercase, because it modifies data. Create a char array and copy your data there, then call lowercase.

Results of string transform are very disappointing. I expected STL to be faster than that.

[edited by - IndirectX on May 26, 2002 6:45:56 PM]
---visit #directxdev on afternet <- not just for directx, despite the name

This topic is closed to new replies.

Advertisement