C++

isupper() C++

Today, we are going to learn one of the functions of C++ which is the isupper() function. We will learn how to implement this function in C++ language. But before that, let’s take a quick look at the basics of C++ and the functions of C++ language so that no point of confusion will be left for the user.

The C++ language foundation is based on the principles of object-oriented programming (OOP). The user can construct and comprehend the concepts of the program with ease because C++ provides a clear structure. Furthermore, the concept has been made explicit by the function in C++ because functions are small blocks of code and we may apply it anywhere in an existing program.

Introduction

The isupper() function is one of the important functions in C++ language and also it is a predefined function in C++ so we don’t have to write dozens of lines to implement the isupper() function. The isupper() function is used to check whether the string or character is in upper-case letters or not. In C++ language, the capitalized alphabets from A to Z have ASCII values from 65 to 90. If the input string or character value cannot be represented as an unsigned char or is not to EOF(End of File), the behavior of the isupper() function is undefinable.

Syntax:

Now, let’s discuss the writing style and the implementation of the isupper() function. First, we will take the integer type and then we will write the function name which is the isupper() function. Then, we will pass an integer-type argument in the function brackets.

Parameters:

arg: it is the input string value or character value to be checked whether it is in upper case letters or not, cast to an int or EOF.

Return Value:

In return, we will get 1 if the input string or character value is in uppercase letters and 0 otherwise.

Example 01:

Now, we will write the very first and simple example of isupper() which we will implement in C++ language. To start writing, we first need a C++ IDE so that we can write and execute the program. For that, open the C++ compiler and start implementing the program.

After opening the compiler, we always start by including the fundamental program modules. These modules are C++’s prepackaged modules of C++ language. Instead of writing dozens of lines of code to generate the module, we only need to type one line of code to include these modules. The “#” indicator instructs the translator to load the module first and then the module is included in the program by using the term “include”.

After this, we will write the module name “iostream,” which means accepting data from the user and showing it to the user. Then, we will include the second module of the program which is the “#include <cctype” module because we have to use the character function in the existing program. Next, we used the “using namespace std” directive to prevent objects, methods, and parameters from referencing the same scope throughout the entire program.

#include <iostream>

#include <cctype>

using namespace std;

int main()
{
    char ch = 'A';
    if (isupper(ch))
        cout<<ch<<" is an uppercase letter";
    else
        cout<<ch<<" is not an uppercase letter";
    return 0;
}

Next, we will start the main function so that we can implement the actual logic or problem of the program. Open the main() function bracket and start writing the code. In line 8, we have declared the character type variable and initialized the character value to it. The character type variable always stores in single or double quotation marks. We have started the if-else statement to check whether the character variable is in an upper-case letter or not. For that, we passed the isupper()function into the if-else. And then we printed it by using the cout() method. The cout() method is the predefined method in C++ language. We will return 0 to the main function which means that the program will run successfully and accomplish its goal.

Now, let's see the output of the above describe illustration:

Example 02:

Let’s start writing the second example of the isupper() function. To use the operations in the existing program, we usually include the program-related modules first. For instance, we must employ the cout() declaration if we intend to display the program. So, we will use the “iostream” package or input the data and output the data of the program. Then, we add one more module to use the character function in the program. For that, we will use the “cctype” module. Then, we will use the “namespace std” directive to not refer to the same name in the whole program.

#include <iostream>

#include <cctype>

using namespace std;

int main ()
{
  char ch[20] = "LinUX";
  int count = 0;
  int i = 0;

  while(ch[i])
  {
    if(isupper(ch[i]))
    count++;
i++;
  }

cout<<"There are " << count << " uppercase letters in "<<ch<< ".";  
  return 0;
}

Then, we will start the main() function so that we can write the actual code of the program. In line 8, we have declared the character type variable “ch” of size 20 and which holds the “LinUX” string value. We have declared two more variables of type integer which are “count” and “i” and we have stored 0 in them.

After this, we used the while loop that will run until the character string “ch[i]” is not null. Then in line 14, checks whether ch[i] is an upper-case string or not. If the first character of the string is upper-case, then it will increment by 1, and so on. When the character string “ch[i] is null then the compiler exits from the while loop. And then prints out the number of upper-case letters of the input character string by using the cout() method. In the end, we will return 0 to the main() so that the execution of the program will terminate and then close the bracket of the main() function.

Let's see the output of the program which we have implemented above:

Conclusion

In this article, we have learned one of the functions of C++ language which is the isupper() function that is used to check whether the input string is in upper case letters or not, and if it is then how many letters are in upper case letters. Then we have also implemented some examples so that we can understand more about the isupper() function and we have explained every line of the example.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.