Explore toupper() Function in C with Examples

Explore toupper() Function in C with Examples

3 mins read376 Views Comment
Updated on Mar 29, 2023 22:36 IST

The toupper() function takes a single character as input and returns the corresponding uppercase character. It is commonly used in conjunction with string manipulation functions to convert entire strings to uppercase.

2023_03_Toupper-C.jpg

In this tutorial, we will discuss the toupper() function in C programming language that is used to convert lowercase C strings into uppercase. We will be covering the following sections:

Before going through, brush up on C programming basics.

Introduction to toupper() Function in C

The toupper() function in C is a standard library function that is used to convert a given lowercase character to its corresponding uppercase character. The toupper() function is part of the ctype.h header file. This function takes a single argument, which is an integer that represents the character to convert.

Here is the syntax for the toupper() function:


 
int toupper(int c);
Copy code

The c argument is the character that is to be converted to uppercase. The function returns the corresponding uppercase character as an integer. 

Have a look at the free C programming courses and pick one up!

C Examples of toupper() Function 

Example 1: Converting a single lowercase character to uppercase


 
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'a';
printf("Before conversion: %c\n", ch);
ch = toupper(ch);
printf("After conversion: %c\n", ch);
return 0;
}
Copy code

Output:

In this example, the toupper() function converts the lowercase character β€˜a’ to its uppercase equivalent. The ch variable is first initialized with the lowercase character β€˜a’, and then passed as an argument to the toupper() function. The resulting uppercase character stores back in the ch variable, and then printed to the console. 

Example 2: Converting a string to uppercase


 
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello, world!";
int i = 0;
while (str[i]) {
putchar(toupper(str[i]));
i++;
}
return 0;
}
Copy code

Output:

In this example, the toupper() function converts each lowercase character in a given string to its uppercase equivalent. The str variable first initializes with the string β€œHello, world!”, and then each character in the string passes as an argument to the toupper() function using a while loop. The resulting uppercase character prints to the console using the putchar() function. 

Example 3: Converting user input to uppercase


 
#include <stdio.h>
#include <ctype.h>
int main() {
char ch;
printf("Enter a lowercase character: ");
scanf("%c", &ch);
ch = toupper(ch);
printf("Uppercase equivalent: %c\n", ch);
return 0;
}
Copy code

Output:

In this example, the toupper() function converts a lowercase character entered by the user to its uppercase equivalent. The scanf() function reads the user input from the console and store it in the ch variable. The toupper() function is then called to convert the lowercase character to uppercase, and the resulting uppercase character is printed to the console. 

Example 4: Comparing characters case-insensitively 


 
#include <stdio.h>
#include <ctype.h>
int main() {
char ch1 = 'A';
char ch2 = 'a';
if (toupper(ch1) == toupper(ch2)) {
printf("The characters are equal (case-insensitive).\n");
} else {
printf("The characters are not equal (case-insensitive).\n");
}
return 0;
}
Copy code

Output:

In this example, the toupper() function converts both ch1 and ch2 to uppercase before comparing them. This allows us to compare the characters case-insensitively, since the uppercase and lowercase versions of the same letter are treated as equal. The if statement tests whether the two characters are equal (case-insensitive), and prints the appropriate message to the console. 

Example 5: Converting characters in an array to uppercase 


 
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "Hello, World!";
int i;
for (i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}
printf("The string in uppercase is: %s\n", str);
return 0;
}
Copy code

Output:

In this example, the toupper() function converts each character in the str array to uppercase. The for loop iterates through each character in the array, calling toupper() on each character and assigning the resulting uppercase character back to the array. Finally, the resulting uppercase string prints to the console using printf()

Endnotes

The toupper() function is a simple yet powerful tool that can help to make C programming more efficient and effective. In this article, we have covered the basics of the toupper() function and provided several examples of how it can be used in C programs.

Don’t forget to check out some good certifications with C programming courses today!

Contributed by Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio