top

Search

C Tutorial

.

UpGrad

C Tutorial

Toupper Function in C

C is a procedural-oriented general-purpose programming language. This high-level language is used to develop computer software, web development, system programming, computer applications, etc. This is a simple and efficient programming language. In C programming some predefined functions are included and stored in the system library. toupper() is one of the most used library functions. Here precise information about the toupper function in C is explained i.e toupper function implementation in C, examples related to toupper function, toupper string in C,  return values of toupper() in C, and exceptions of toupper().

What is toupper() in C?

The toupper function is a built-in function that is used to perform some specific operation. toupper function in C language is defined in <ctype.h> header file. <ctype.h> header file includes operations related to characters. toupper() in C header file is used to convert the lowercase alphabet to the uppercase alphabet. It excludes some operations including the conversion of uppercase letters, digits, and special characters. To convert the alphabet of a string from uppercase to lowercase, first, each character of the string is traversed in sequence, and then the toupper function is applied to every individual character of the string.

How to Use the toupper() Function in C?

1. If a lowercase alphabet is provided and it needs to be converted to its equivalent uppercase alphabet then toupper() function is used for the conversion

2. The toupper function first considers the ASCII value of the character. After that, it returns the equivalent uppercase character in the form of an integer value. 

3. If the inputs to the toupper function consist of the uppercase alphabet, unique character, or digit, then the function returns the original alphabet without any modification.

4. If a string in lowercase is to be converted to uppercase, then all the alphabets of the string are first traversed. Later toupper() is applied to every character separately to convert the alphabet of a string to uppercase.

Toupper function implementation in C

The syntax of toupper() is 

int toupper(int a);

Here the goal is to convert the character ’a’ from lowercase to uppercase. To achieve this toupper function first recognize the ASCII value of the character and then transform the alphabet from lowercase to uppercase.

Example of toupper() in C

Example 1: C program to convert a single lowercase character to an uppercase character.

#include <stdio.h>
#include<ctype.h>
Int main()
{
            char ch = ‘b’;
            printf(“Before implementing toupper function: %c\n”, ch);
            ch = toupper(ch);
            printf (“After implementing toupper function: %c\n”, ch);
            return 0;
}

Output: 

Before implementing toupper function: b
After implementing toupper function: B

Algorithm:

1. Initialize the variable ch with the lowercase character ‘b’.

2. The variable is then passed as an argument to toupper() function.

3. Next the converted uppercase character is stored back in the ch variable.

4. Output is printed.

Example 2 : C program to convert a string to uppercase.

#include <stdio.h>
#include<ctype.h>
Int main()
{
            char ch[ ] = “good morning everyone @123”;
            int i = 0;
            while (ch[ i ])
            {
            putchar (toupper(ch[ i ]));
            i ;
            }
return 0;
}         

Output:

GOOD MORNING EVERYONE @123

Explanation:

1. The string is represented as “good morning everyone @123”.

2. A while loop is used to traverse the string. In this loop toupper function is applied to every character.

3. The lowercase characters are converted to uppercase characters while the symbols and digits remain the same after conversion.

4. Output is printed as GOOD MORNING EVERYONE @123.

Example 3: C program to convert 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 (“After implementing toupper function: %c\n”, ch);
            return 0;
}

Output: 

Enter a lowercase character r
After implementing toupper function: R

Explanation:

1. The user input is read by scanf() function and the input is stored in character variable ch.

2. The toupper() function is called to convert the lowercase character to an uppercase character.

3. Next the converted uppercase character is stored back in the ch variable.

4. Output is printed.

Example 4: C program to compare characters case-insensitively.

#include <stdio.h>
#include<ctype.h>
Int main()
{
            char ch1 = ‘E’;
            char ch2 = ‘e’;
            if (toupper(ch1)==toupper(ch2))
                        {
                                    printf(“Both characters are the same (case-insensitive).\n”);
                        }
            else 
                        { 
                                    printf(“Characters are not same (case-insensitive).\n”);
                        }
            return 0;
}

Output:

Both characters are the same.

Explanation: 

1. The first character ‘E’ is stored in a character variable ch1. The second character ‘e’ is stored in a character variable ch2.

2. First both the characters are converted to uppercase by using the toupper function.

3. The first character ‘E’ is remained as sameE' and the second character ‘e’ is converted to ‘E’.

4. For comparison of characters if a statement  is used. It compares if both the characters are equal or not.

5. Output is printed.

Example 5: C program to convert characters of an array to uppercase.

#include <stdio.h>
#include<ctype.h>
Int main()
{
            char ch[ ] = “Hello, Everyone”;
            int I;
            for (i=0; ch[i] != ‘\0’; i );
            {
            ch[i ] = toupper(ch[ i]);
            }
printf(“The string in uppercase is: %s\n”, ch);
return 0;
}

Output: 

The string in uppercase is: HELLO, EVERYONE

Explanation:

1. Each character of the ch array is converted to uppercase.

2. For loop iterates each character in the array and the toupper() function is applied to each character.

3. Thus the string is converted to uppercase and the result is printed.

Example 6: 

#include <stdio.h>
#include<ctype.h>
Int main()
{
            char ch[ ] = “EXCELLENT”;
            int i = 0;
            while (ch[ i ])
            {
            putchar (toupper(ch[ i ]));
            i ;
            }
return 0;
}

Output: 

EXCELLENT

Explanation: 

The output remains the same as input because the toupper() function does not convert uppercase characters.

Example 7: 

#include <stdio.h>
#include<ctype.h>
Int main()
{
            char ch[ ] = “123@45#”;
            int i = 0;
            while (ch[ i ])
            {
            putchar (toupper(ch[ i ]));
            i ;
            }
return 0;
}

Output: 

123@45#”

Explanation: 

The output remains the same as input because the toupper() function does not convert digits and symbols.

Parameters of toupper() in C

int toupper(int m);

1. The input to the toupper function is a single character. Here the character ‘m’ is to be converted to uppercase. 

2. The toupper function reads the ASCII value of the character and converts it from lowercase to uppercase.

3. This function is usually operated in conjunction with string manipulation functions to convert a whole string from lowercase to uppercase.

Return Values of toupper() in C

The toupper() returns an uppercase character corresponding to the lowercase character which is given as input to the function.

Exceptions of toupper() in C

The exceptions of toupper() in C are listed below.

1. When an uppercase character is given as input to the toupper function then it returns the same original character without any modification.

2. When a special symbol or digit is given as input to the toupper function then it returns the same original character.

tolower in C:

The tolower() function converts the uppercase characters into lowercase characters. Syntax of tolower() function is 

int tolower(int a);

The difference between toupper() and tolower() functions is that the toupper function is a builtin function that is used to convert the lowercase character to a uppercase character, whereas the tolower() function converts the uppercase characters into lowercase characters.

Conclusion

Detailed information about the use of the toupper function is provided here. The significance of toupper(), the syntax used to define toupper() are explained. The implementation of the toupper function in the C programming language is clearly described. The parameters used in toupper function, return value, and exceptions while using toupper function are also elaborated. Implementation of toupper function in C language is clearly depicted by using various examples. The examples are based on converting a single lowercase character to an uppercase character,  converting a string to uppercase, comparison of characters case-insensitively, and conversion of characters of an array to uppercase.  The simple yet powerful toupper() function makes C programming easier and more efficient.  

FAQs

1. What is C language?

C is a procedural-oriented general-purpose programming language. This high-level language is used to develop computer software, web development, system programming, computer applications, etc.

2. What is toupper() in C?

The toupper function is a built-in function that is used to convert the lowercase alphabet to the uppercase alphabet.

3. What is <ctype.h>?

<ctype.h> header file includes operations related to characters. toupper function in C language is defined in <ctype.h> header file.

4. What is the syntax of toupper()?

The syntax of toupper() is 

int toupper(int a);

Here the goal is to convert the character ’a’ from lowercase to uppercase.

5. How does a toupper() function work?

If a string in lowercase is to be converted to uppercase, then all the alphabets of the string are first traversed. Later toupper() is applied to every character separately to convert the alphabet of a string to uppercase.

6. What are the return Values of toupper() in C?

The toupper() returns an uppercase character corresponding to the lowercase character which is given as input to the function.

7. What are the exceptions of toupper() in C

The exceptions of toupper() in C are listed below.

i) When an uppercase character is given as input to the toupper function then it returns the same original character without any modification.

ii) When a special symbol or digit is given as input to the toupper function then it returns the same original character.

8. What is tolower in c?

The tolower() function converts the uppercase characters into lowercase characters. Syntax of tolower() function is

int tolower(int a);

9. What is the difference between toupper and tolower in C?

The toupper function is a built-in function that is used to convert the lowercase character to the uppercase character, whereas the tolower() function converts the uppercase characters into lowercase characters.

Leave a Reply

Your email address will not be published. Required fields are marked *