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. Show Example#include <iostream> #include <cctype> using namespace std; int main() {// convert 'a' to uppercase char ch = toupper('a'); cout << ch; return 0; } // Output: AThe syntax of the toupper() function is: toupper(int ch);toupper() ParametersThe toupper() function takes the following parameter:
toupper() Return ValueThe toupper() function returns:
toupper() PrototypeThe 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 BehaviorThe behaviour of toupper() is undefined if:
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 9Here, 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:
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 57Here, 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:
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. Parameter: It accepts a single parameter:
Returns: This function returns the uppercase character corresponding to the ch.Below programs illustrate the toupper() function in C: Example 1:-
Output: g in uppercase is represented as G Example 2:-
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 :
Output:
Article Tags : 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.
SyntaxThe function takes in an alphabet as a parameter and returns that character in uppercase. See the function definition below: int toupper(int ch);
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 |