How do I lowercase a string in C?

How can I convert a mixed case string to a string string in C?

+91
c string lowercase
Apr 18 2018-10-18T00:
source share
5 answers

This is in the standard library, and this is the most direct way that I see to implement such a function. So yes, just loop over the string and convert each character to lowercase.

Something like that

#include <ctype.h> for(int i = 0; str[i]; i++){ str[i] = tolower(str[i]); } 

or, if you prefer a single liner, you can use this variant of JF Sebastian:

 for ( ; *p; ++p) *p = tolower(*p); 
+135
Apr 18 '10 at 9:44
source share
— -

to convert to lowercase is equivalent to the 0x60 overclock bit:

 for(char *p = pstr;*p;++p) *p=*p>0x40&&*p<0x5b?*p|0x60:*p; 

(for the latin codepage, of course)

+6
Apr 18 '10 at
source share

Do you just deal with ASCII strings and have no locality issues? Then yes, that would be a good way to do this.

+1
Apr 18 2018-10-18T00:
source share

If you need lower case Unicode support, check out this question: Light Unicode Library

+1
Nov 19 '10 at 19:43
source share

If we are not careful about using tolower() , do the following:

 char blah[] = "blah blah Blah BLAH blAH\0"; int i=0; while(blah[i]|=' ', blah[++i]) {} 

But, well, it kind of explodes if you feed it with some symbols / numbers, and in general it is evil. Good interview question.

+1
May 22 '13 at 5:14
source share



All Articles