C strlwr() Function
The strlwr()
function in C is used to convert all uppercase characters in a string to lowercase. This function is part of some C libraries like the C Run-Time Library (CRT), but it is not part of the standard C library.
Syntax of strlwr()
The syntax of the strlwr()
function is as follows:
Syntax:
char *strlwr(char *str);
The function takes one argument:
- str: A pointer to the string whose characters will be converted to lowercase. The string is modified in place.
The function returns a pointer to the modified string with all lowercase characters.
Example of strlwr() Function
Here is an example that demonstrates how the strlwr()
function works. Since it is not part of the standard C library, you may need to implement it or use a compatible library.
Example:
#include <stdio.h>
#include <ctype.h>
// For the standard library functions
// Function to convert string to lowercase (implementation of strlwr)
char* strlwr(char* str) {
for (int i = 0; str[i]; i++) {
if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = str[i] + ('a' - 'A'); // Convert to lowercase
}
}
return str;
}
int main() {
char str[] = "HELLO WORLD!";
printf("Original String: %s\n", str);
// Convert the string to lowercase
strlwr(str);
printf("Lowercase String: %s\n", str);
return 0;
}
Output:
Original String: HELLO WORLD!
Lowercase String: hello world!
Lowercase String: hello world!
Explanation of the Example
In this example:
- The function
strlwr()
is manually implemented to convert each uppercase character in the string to lowercase. - A loop iterates through each character in the string, and if a character is uppercase (between 'A' and 'Z'), it is converted to lowercase by adding the difference between 'a' and 'A'.
- The modified string is printed after conversion to lowercase.
Important Notes
- The
strlwr()
function is not part of the standard C library, so it may not be available in all C environments. - The function modifies the original string in place, so the original string is changed.
- Ensure that the string is null-terminated to avoid undefined behavior.
- Some C compilers or platforms may provide this function as part of their non-standard libraries (e.g., MSVC). If it's not available, you can implement it as shown in the example.