sepiroth sepiroth - 27 days ago 16
C Question

c - convert a mixed-case string to all lower case

What is the best/cleanest way to convert a mixed case string to a lowercase one in C?

Answer

It isn't 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:

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);