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>.
strlwr() as a compiler-specific extension.strlwr(string); // non-standard extension
#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().
#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;
}
#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;
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.