To change a lower case character, named ch, to an upper case character, use this function.

Question 1To change a lower case character, named ch, to an upper case character, use this function.Select one:a. atoi(ch)b. itoa(ch)c. toupper(ch) CorrectCorrectd. ltou(ch)e. None of theseThe correct answer is: toupper(ch)
19.To change a character argument from lower to upper case, use this function.a.isupperb.toupperc.tolarged.fromlowere.None of theseANS: B

The toupper() function in C++ converts a given character to uppercase. It is defined in the cctype header file.

Example

#include <iostream> #include <cctype> using namespace std; int main() {

// convert 'a' to uppercase char ch = toupper('a');

cout << ch; return 0; } // Output: A

The syntax of the toupper() function is:

toupper(int ch);

toupper() Parameters

The toupper() function takes the following parameter:

  • ch - a character, casted to int type or EOF

toupper() Return Value

The toupper() function returns:

  • For Alphabets - the ASCII code of the uppercase version of ch
  • For Non-Alphabets - the ASCII code of ch

toupper() Prototype

The function prototype of toupper() as defined in the cctype header file is:

int toupper(int ch);

As we can see, the character argument ch is converted to int i.e. its ASCII code.

Since the return type is also int, toupper() returns the ASCII code of the converted character.

toupper() Undefined Behavior

The behaviour of toupper() is undefined if:

  • the value of ch is not representable as unsigned char, or
  • the value of ch is not equal to EOF.

Example 1: C++ toupper()

#include <cctype> #include <iostream> using namespace std; int main() { char c1 = 'A', c2 = 'b', c3 = '9';

cout << (char) toupper(c1) << endl; cout << (char) toupper(c2) << endl; cout << (char) toupper(c3);

return 0; }

Output

A B 9

Here, we have converted the characters c1, c2, and c3 to uppercase using toupper().

Notice the code for printing the output:

cout << (char) toupper(c1) << endl;

Here, we have converted the return value of toupper(c1) to char using the code (char) toupper(c1).

Also notice that initially:

  • c2 = 'A', so toupper() returns the same value
  • c3 = '9', so toupper() returns the same value

Example 2: C++ toupper() without Type Conversion

#include <cctype> #include <iostream> using namespace std; int main() { char c1 = 'A', c2 = 'b', c3 = '9';

cout << toupper(c1) << endl; cout << toupper(c2) << endl; cout << toupper(c3);

return 0; }

Output

65 66 57

Here, we have converted the characters c1, c2, and c3 to uppercase using toupper().

However, we have not converted the returned values of toupper() to char. So, this program prints the ASCII values of the converted characters, which are:

  • 65 - the ASCII code of 'A'
  • 66 - the ASCII code of 'B'
  • 57 - the ASCII code of '9'

Example 3: C++ toupper() with String

#include <cctype> #include <iostream> #include <cstring> using namespace std; int main() { char str[] = "John is from USA."; char ch; cout << "The uppercase version of \"" << str << "\" is " << endl; for (int i = 0; i < strlen(str); i++) {

// convert str[i] to uppercase ch = toupper(str[i]);

cout << ch; } return 0; }

Output

The uppercase version of "John is from USA." is JOHN IS FROM USA.

Here, we have created a C-string str with the value "John is from USA.".

Then, we converted all the characters of str to uppercase using a for loop. The loop runs from i = 0 to i = strlen(str) - 1.

for (int i = 0; i < strlen(str); i++) { ... }

In other words, the loop iterates through the whole string since strlen() gives the length of str.

In each iteration of the loop, we convert the string element str[i] (a single character of the string) to uppercase and store it in the char variable ch.

ch = toupper(str[i]);

We then print ch inside the loop. By the end of the loop, the entire string has been printed in uppercase.

The toupper() function is used to convert lowercase alphabet to uppercase. i.e. If the character passed is a lowercase alphabet then the toupper() function converts a lowercase alphabet to an uppercase alphabet. It is defined in the ctype.h header file.
Syntax: 
 

int toupper(int ch);

Parameter: It accepts a single parameter: 

  • ch: This represents the character to be converted to uppercase.

Returns: This function returns the uppercase character corresponding to the ch.Below programs illustrate the toupper() function in C:

Example 1:- 

    printf("%c in uppercase is represented as  %c",

Output:  g in uppercase is represented as G

Example 2:- 



    char str[] = "geekforgeeks\n";

Note:  

 If the character passed in the toupper() is any of these three

1. uppercase character

2. special symbol

3. digit

toupper() will return the character as it is.
                      

Example :

    char str[] = "GeEks@123\n";

Output:

GEEKS@123


Article Tags :

To change a lower case character, named ch, to an upper case character, use this function.
Educative Answers Team

In C, the toupper() function is used to convert lowercase alphabets to uppercase letters.

When a lowercase alphabet is passed to the toupper() function it converts it to uppercase.

When an uppercase alphabet is passed to the function it returns the same alphabet.

Note: A ctype.h header file needs to be included in order to use this function.

Syntax

The function takes in an alphabet as a parameter and returns that character in uppercase.

See the function definition below:

int toupper(int ch);

Note: The character is passed in and returned as int; i.e, the ASCII value of the character moves around.

Let’s start with the most basic example of converting a lower case alphabet to uppercase using this function.

See the code below:

#include <ctype.h> int main() { char ch; // letter to convert to uppercase ch = 'a'; printf("%c in uppercase is represented as %c", ch, toupper(ch)); return 0; }

Simple, right?

Now, let’s talk about whole words or sentences. How can you convert them to uppercase using this function? You cannot simply pass the whole word to the function as toupper() only takes one character at a time.

To accomplish this, you would need to loop over the whole word/sentence using a while loop; thus individually converting each character to uppercase.

The process is illustrated in the diagram below:

Now let’s put this in code:

#include <ctype.h> int main() { // counter for the loop int i = 0; // word to convert to uppercase char word[] = "edUcaTivE\n"; char chr; // Loop while (word[i]) { chr = word[i]; printf("%c", toupper(chr)); i++; } return 0; }

Each alphabet character of the word is passed to the toupper() function, and its uppercase form is returned and displayed.

RELATED TAGS

Copyright ©2022 Educative, Inc. All rights reserved