Strlwr Function In C

Syntax:

It has the following syntax:

Example

char* strlwr(char* str);

It refers to the string that needs to be in lowercase and terminated with a null character.

Value Returned:

The strlwr function returns the same pointer str, which now points to the modified lowercase text.

The strlwr(string) function converts all characters in a string to lowercase. Here is a straightforward demonstration showcasing the functionality of the strlwr function.

Example

Example

#include<stdio.h>

#include <string.h>  

int main(){  

  char str[20];  

  printf("Enter string: ");  

  gets(str);//reads string from console  

  printf("String is: %s",str);  

  printf("\nLower String is: %s",strlwr(str));  

 return 0;  

}

Output

Output

Enter string: LOGIC PRACTICEtech

String is: LOGIC PRACTICEtech

Lower String is: logic practice

Explanation:

The necessary header files are initially added to the code: stdio.h for handling input/output tasks and string.h for operations related to string manipulation.

The input string will be stored in an array named str with a length of 20 characters.

The printf function prompts the user to supply a string.

The gets function reads the user input string. It's important to note that gets is considered unsafe and should be replaced with fgets to mitigate buffer overflow risks. The printf function displays the initial input string.

Once this is done, the str pointer is passed as a parameter while invoking the strlwr function. This function then returns the modified string after converting all uppercase characters within the string to lowercase.

Following that, the printf function is employed to display the lowercase string.

Problems with Safer Input Reading and gets

The usage of the gets function in the example highlights a significant concern related to buffer overflow susceptibility. This function lacks input size validation, potentially leading to memory corruption. It is highly recommended to transition from gets to fgets for more secure input handling. The modified code snippet would appear as below:

Example

Example

#include <stdio.h>

#include <string.h>



int main() {

char str[20];



printf("Enter string: ");

fgets(str, sizeof(str), stdin);



// Rest of the code remains unchanged.

}

Output

Output

Enter string: HELLOworld

String is: HELLOworld

Lower String is: helloworld

Conclusion:

In summary, the strlwr function in the C programming language provides a convenient method for converting uppercase characters to lowercase within a string. This functionality facilitates case-insensitive operations and ensures dependable string manipulation. Although widely utilized, it is essential to note that the strlwr function is non-standard and may not be present in every C implementation, thereby posing concerns about code portability.

The sample code was cautioned against utilizing gets, with the safer alternative fgets recommended as a solution to address possible security vulnerabilities. It is crucial to prioritize secure input management to prevent issues like buffer overflow and memory corruption.

While strlwr is capable of achieving its objective, developers must understand its constraints and explore alternative methods for manipulating strings that comply with standards. Utilizing custom functions in different C environments may offer greater portability when converting strings to lowercase.

Mastering string manipulation in C, particularly effectively utilizing functions such as strlwr, enables developers to efficiently handle strings and craft dependable, secure, and universally compatible software.

Input Required

This code uses input(). Please provide values below: