🎓 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 toupper() function in C is a standard library function that converts a given lowercase letter to its corresponding uppercase letter. It is part of the C standard library (ctype.h). This function is useful for normalizing text by converting all letters to uppercase.
Table of Contents
- Introduction
toupper()Function Syntax- Examples
- Converting a Lowercase Letter to Uppercase
- Using
toupper()with User Input
- Real-World Use Case
- Conclusion
Introduction
The toupper() function converts a given lowercase letter to its corresponding uppercase letter. If the character passed to the function is not a lowercase letter, it returns the character unchanged.
toupper() Function Syntax
The syntax for the toupper() function is as follows:
#include <ctype.h>
int toupper(int c);
Parameters:
c: The character to be converted, which is passed as anint.
Returns:
- The function returns the uppercase equivalent of the character if it is a lowercase letter. Otherwise, it returns the character unchanged.
Examples
Converting a Lowercase Letter to Uppercase
To demonstrate how to use toupper() to convert a lowercase letter to uppercase, we will write a simple program.
Example
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'g';
// Convert the character to uppercase
char result = toupper(ch);
// Print the result
printf("Uppercase of '%c' is '%c'.\n", ch, result);
return 0;
}
Output:
Uppercase of 'g' is 'G'.
Using toupper() with User Input
This example shows how to use toupper() to convert a character provided by the user to uppercase.
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 uppercase
char result = toupper(ch);
// Print the result
printf("Uppercase of '%c' is '%c'.\n", ch, result);
return 0;
}
Output (example user input 'b'):
Enter a character: b
Uppercase of 'b' is 'B'.
Output (example user input 'B'):
Enter a character: B
Uppercase of 'B' is 'B'.
Real-World Use Case
Normalizing User Input for Case-Insensitive Comparison
In real-world applications, the toupper() function can be used to normalize user input, converting all letters to uppercase for case-insensitive comparison.
Example: Comparing Strings Case-Insensitively
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void to_uppercase(char* str) {
for (int i = 0; str[i] != '\0'; i++) {
str[i] = toupper(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 uppercase
to_uppercase(str1);
to_uppercase(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 toupper() function is essential for converting lowercase letters to uppercase 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