Q:

Is it possible to find uppercase letters and replace them to lowercase?

If my_array[50] has randomly uppercase letters inside, is there any way to find the find them and switch them to lowercase letters? I am really looking for an implementation-independent method because it is going to be cross platform. The code will be used on other languages too.

A:

C provides a tolower function that converts uppercase letters to lowercase letters and leaves other characters unchanged. It is affected by the current locale (it will use different alphabets depending on the locale you set with the setlocale function). It should be used with unsigned char characters.
#include <ctype.h>
#include <stdlib.h>

/*  Given length characters starting at p,
    convert uppercase letters to lowercase.
*/
void ToLower(size_t length, unsigned char *p)
{
    for (size_t i = 0; i < length; ++i)
        p[i] = tolower(p[i]);
}

(For “wide characters,” the function towctrans provides a similar operation. It is more complicated to use.)

