strlwr() and Portable Lowercase Conversion in C

strlwr() converts letters in a string to lowercase on implementations that provide it, but it is not an ISO C standard-library function. Portable C code can use tolower() from <ctype.h>.

Portability: treat strlwr() as a compiler-specific extension.

Legacy Syntax Top ↑

strlwr(string);  // non-standard extension

Portable Lowercase Function Top ↑

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

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

int main(void) {
    char text[] = "PLUS2NET.COM";

    to_lowercase(text);
    printf("%s\n", text);

    return 0;
}

Output

plus2net.com

The cast to unsigned char avoids undefined behavior for negative plain-char values passed to tolower().

Lowercase User Input Top ↑

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

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

int main(void) {
    char text[80];

    printf("Enter text: ");
    if (fgets(text, sizeof text, stdin) == NULL) {
        return 1;
    }
    text[strcspn(text, "\n")] = '\0';

    to_lowercase(text);
    printf("Lowercase: %s\n", text);

    return 0;
}

Preserved Example: Convert an Array of Strings to Lowercase Top ↑

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

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

int main(void) {
    char words[][20] = {"HELLO", "WORLD", "PLUS2NET"};
    size_t count = sizeof words / sizeof words[0];

    for (size_t i = 0; i < count; i++) {
        to_lowercase(words[i]);
        printf("Lowercase: %s\n", words[i]);
    }

    return 0;
}




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer