HOWTO · C

How to Convert String to Lowercase in C

Learn safe ways to convert a null-terminated C string to lowercase with tolower, including in-place conversion, separate allocated results, pointer traversal, and byte-oriented limits.

On this page

To convert a C string to lowercase, include <ctype.h> and apply tolower to each byte until the null terminator. Convert each input to unsigned char before passing it to tolower. Choose an in-place loop only when you own a writable character array; otherwise, create a separate result and release it with free when finished.

C strings are null-terminated byte sequences. A pointer to a string does not say whether its storage can be changed or who owns it, so choose the output strategy from those facts. For case-insensitive comparison, also remember that converting to lowercase is not a substitute for a comparison routine designed for the data and locale you use.

Choose whether to modify the input or create a separate result

Use in-place conversion when the input is a writable array and the caller wants that array changed. A separate result is appropriate when the input must stay intact, comes from read-only storage, or is needed in both original and converted forms. The separate-result approach needs an allocation check and a clear owner for the returned memory.

A string literal such as "Hello" must not be modified; attempting to write to it has undefined behavior. A pointer declared as char * does not make the pointed-to bytes writable. Use an array initialized from the literal, such as char word[] = "Hello", when conversion should change the characters.

Convert characters with tolower safely

tolower is declared in <ctype.h> and converts one character value according to the active C locale. Its argument must be either EOF or a value representable as unsigned char. On systems where plain char is signed, passing a negative char value directly can cause undefined behavior. The cast must happen before the call: tolower((unsigned char)text[i]).

Run this example with a C compiler:

#include <ctype.h>
#include <stdio.h>

int main(void) {
    char text[] = "Hello, World!";

    for (size_t i = 0; text[i] != '\0'; ++i) {
        text[i] = (char)tolower((unsigned char)text[i]);
    }

    printf("%s\n", text);
    return 0;
}

The supplied runtime evidence records a passing execution of this complete program. It uses a modifiable array, visits characters only up to the terminator, and passes a valid argument value to tolower.

Convert a string in place

The example above converts its text array in place: each lowercase result replaces the character at the same position, and the terminating null byte remains in place. This requires storage with enough writable capacity for the existing string. It does not change a string literal or an unrelated source buffer.

If you need to preserve the original, do not run the loop against that storage. Copy the bytes into a writable destination first, ensuring it has room for the terminator, or use the separately allocated approach below. Merely printing the result of tolower for each character would display converted characters but would not modify the source string.

Convert into a separately allocated string

A separate result keeps the input unchanged. Allocate strlen(input) + 1 bytes, since the output has the same byte count plus the null terminator. Check whether malloc returned NULL before writing to the destination. The caller owns a successful result and must eventually pass it to free.

The following complete example reports allocation failure and frees the result after use. Its input is a string literal, which is safe because the function reads from it and writes only to the allocated destination.

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char *lowercase_copy(const char *input) {
    size_t length = strlen(input);
    char *result = malloc(length + 1);

    if (result == NULL) {
        return NULL;
    }

    for (size_t i = 0; i < length; ++i) {
        result[i] = (char)tolower((unsigned char)input[i]);
    }
    result[length] = '\0';
    return result;
}

int main(void) {
    const char *input = "THIS STRING STAYS UNCHANGED";
    char *lower = lowercase_copy(input);

    if (lower == NULL) {
        fputs("Could not allocate result.\n", stderr);
        return 1;
    }

    printf("%s\n", input);
    printf("%s\n", lower);
    free(lower);
    return 0;
}

The supplied runtime evidence records a passing execution of this complete program, with output matching the unchanged input followed by its lowercase copy. If an allocation fails, the helper returns NULL; the caller checks that result before printing or freeing it. The helper expects input to point to a valid null-terminated string.

Wrap the conversion in a reusable function

The lowercase_copy helper above separates conversion from the caller and documents a useful ownership contract: it returns a newly allocated string, or NULL if allocation fails. The caller must not free the original input through this result, and must free the returned pointer exactly once when it is no longer needed. If the helper accepts data that may not be null-terminated, change its interface to take a length and define how the output terminator is handled; strlen cannot safely determine the length of an unterminated buffer.

For an in-place helper instead, make the writable-buffer requirement explicit in its parameter contract. A char * parameter alone cannot enforce that requirement, so callers still must not pass a string literal or other read-only storage.

Traverse the string with a pointer

Pointer traversal performs the same conversion without indexing. The pointer begins at the first byte and advances until it points to the null terminator. Each dereferenced value still needs the unsigned char cast before calling tolower, and the pointed-to array must be writable.

For example, the loop body can be written as while (*p != '\0') { *p = (char)tolower((unsigned char)*p); ++p; }. This is an alternate loop form, not a way to make read-only storage writable; use it only with a mutable character array.

Understand character-set and locale limits

tolower is byte-oriented and follows the active C locale. In the default C locale, it handles the basic letters in the execution character set; other locale settings may define additional single-byte mappings. It does not decode UTF-8 or perform general Unicode case conversion. A non-ASCII UTF-8 character can span multiple bytes, and converting those bytes independently does not reliably lowercase that character. Use a Unicode-aware library when the input is Unicode text and full case conversion is required.

The two complete programs above passed in the supplied runtime evidence. The pointer-loop fragment is illustrative and was not included in those executions, and no particular locale behavior is asserted. The casts and loops demonstrate the standard C argument and string-termination rules; they do not promise language-independent text conversion.