🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The tolower() function in C is a standard library function that converts a given uppercase letter to its corresponding lowercase letter. It is part of the C standard library (ctype.h). This function is useful for normalizing text by converting all letters to lowercase.
Table of Contents
- Introduction
tolower()Function Syntax- Examples
- Converting an Uppercase Letter to Lowercase
- Using
tolower()with User Input
- Real-World Use Case
- Conclusion
Introduction
The tolower() function converts a given uppercase letter to its corresponding lowercase letter. If the character passed to the function is not an uppercase letter, it returns the character unchanged.
tolower() Function Syntax
The syntax for the tolower() function is as follows:
#include <ctype.h>
int tolower(int c);
Parameters:
c: The character to be converted, which is passed as anint.
Returns:
- The function returns the lowercase equivalent of the character if it is an uppercase letter. Otherwise, it returns the character unchanged.
Examples
Converting an Uppercase Letter to Lowercase
To demonstrate how to use tolower() to convert an uppercase letter to lowercase, we will write a simple program.
Example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'G';
// Convert the character to lowercase
char result = tolower(ch);
// Print the result
printf("Lowercase of '%c' is '%c'.\n", ch, result);
return 0;
}
Output:
Lowercase of 'G' is 'g'.
Using tolower() with User Input
This example shows how to use tolower() to convert a character provided by the user to lowercase.
Example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch;
// Get user input for the character
printf("Enter a character: ");
scanf("%c", &ch);
// Convert the character to lowercase
char result = tolower(ch);
// Print the result
printf("Lowercase of '%c' is '%c'.\n", ch, result);
return 0;
}
Output (example user input 'B'):
Enter a character: B
Lowercase of 'B' is 'b'.
Output (example user input 'b'):
Enter a character: b
Lowercase of 'b' is 'b'.
Real-World Use Case
Normalizing User Input for Case-Insensitive Comparison
In real-world applications, the tolower() function can be used to normalize user input, converting all letters to lowercase for case-insensitive comparison.
Example: Comparing Strings Case-Insensitively
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void to_lowercase(char* str) {
for (int i = 0; str[i] != '\0'; i++) {
str[i] = tolower(str[i]);
}
}
int main() {
char str1[100], str2[100];
// Get user input for the strings
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
// Convert both strings to lowercase
to_lowercase(str1);
to_lowercase(str2);
// Compare the strings
if (strcmp(str1, str2) == 0) {
printf("The strings are equal (case-insensitively).\n");
} else {
printf("The strings are not equal (case-insensitively).\n");
}
return 0;
}
Output (example user input str1 "Hello" and str2 "hello"):
Enter the first string: Hello
Enter the second string: hello
The strings are equal (case-insensitively).
Output (example user input str1 "Hello" and str2 "world"):
Enter the first string: Hello
Enter the second string: world
The strings are not equal (case-insensitively).
Conclusion
The tolower() function is essential for converting uppercase letters to lowercase in C. It is useful in various applications, particularly in fields like data normalization and text processing, where it is necessary to ensure consistent letter casing.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment