How do I lowercase a string in C?

Tony Stark picture Tony Stark · Apr 18, 2010 · Viewed 258.7k times · Source

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

Answer

Earlz picture Earlz · Apr 18, 2010

It's in the standard library, and that's the most straight forward way I can see to implement such a function. So yes, just loop through the string and convert each character to lowercase.

Something trivial like this:

#include <ctype.h>

for(int i = 0; str[i]; i++){
  str[i] = tolower(str[i]);
}

or if you prefer one liners, then you can use this one by J.F. Sebastian:

for ( ; *p; ++p) *p = tolower(*p);