C toupper() Function

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

  1. Introduction
  2. toupper() Function Syntax
  3. Examples
    • Converting a Lowercase Letter to Uppercase
    • Using toupper() with User Input
  4. Real-World Use Case
  5. 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 an int.

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.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare