We will discuss using the isalpha() function in the C++ examples. Let’s make a fresh start by opening Ubuntu’s console shell. Without a C++-type file, we will not be able to implement any code. Thus, we used the following “touch” query on the shell, and the file was created in a second. After this, open it within Ubuntu’s Nano editor using its shell console. Try the following “nano” instruction with the new file name:
Our newly generated file with the touch instruction has been successfully opened in the shell’s “nano” editor. We will start today’s article with the simplest example for the “isalpha” function in C++. As we know, the “isalpha” function belongs to the character family of C++, so we need to use its respective library in our code. We have used the “iostream” library of C++, which is necessary to use standard input and output streams. After this, include the “cctype” library to use character values, variables, and functions.
Without this library, our “isalpha” function won’t work. The C++ program always executes itself from its main() function. So, we have added the main() function implementation after both the libraries. At the first line of the main() function, we have initialized an integer variable “n” with the “isalpha” function taking the character “m” in its parameter. This function will look at the character if it is an alphabet or not and store the result within the “n” variable. On the very next consecutive line, we have been using the standard “cout” object to display the result we have in the variable “n”. Our main() function and the code are now completed and ready to be compiled on the shell, as shown below:
To compile C++ code in Ubuntu’s shell application, you must have the “g++” compiler configured on your system already. Without it, Linux users will not be able to compile their C++ code. So, we have used the already installed “g++” compiler to compile our isalpha.cc file with the g++ instruction as shown below. Our compilation was successful as it didn’t return anything. The “a./.out” command in a Linux execution instruction for different programming files. So, we have used it to execute our compiled “isalpha.cc” file. In return, we have a non-zero numerical value. This illustrates that the value “m” is the alphabet.
Let’s update the C++ code and use the numerical value within the “isalpha” function. We have opened the same file and replaced the “m” alphabet with the number “4” in the parameters of the “isalpha” function. We have not changed the overall code other than that. Now, it must be saved before the next compilation to see any change in the result. So, we have tried the shortcut “Ctrl+S” on the Gnu editor, and the file is now saved as updated.
Exit the C++ code file with the “Ctrl+X” shortcut and compile it again with the g++ compiler. On execution of updated code, we have 0 as an output. This shows the value-added in the isalpha function was not an alphabet.
Within the first example, we have used the integer variable to store the result of the isalpha function. The same thing can be achieved with the Boolean variable with more accuracy. Using Boolean variables, we will only get 0 for non-alphabet and 1 for alphabet value, i.e., more accurate. So, we updated the last code. Initialized two Boolean variables, n1 and n2, that are using the isalpha() function to check one integer and one alphabetical value. The results will be shown at two different lines using the cout objects with a standard namespace. Save this program using the Ctrl+S shortcut and see what will be shown on the shell.
The compilation was successful using the “g++” instruction. On running the “./a.out” instruction, we have 1 as “b” is alphabet and 0 for “7”.
There is another method in C++ to get the result of the isalpha() function as a Boolean value, i.e., true or false. For this, we need to initialize the unsigned character variables with some values. We have used two unsigned variables, n1 and n2, to initialize one alphabetical and one numerical value. Within the cout statements, we have used the boolalpha object and the isalpha() function. The isalpha function returned value has been converted into a Boolean form using the “bool” keyword. In other words, the variables will be passed to the isalpha() function to check if their value is an alphabet or not. The returned 0 or 1 value would be converted into false and true, respectively. This method is unique and different from the previous examples. Let’s save the code to see its result.
On compilation and execution of new C++ code, we have the output in true and false as a return to the isalpha() function.
Let’s look at the last example of using the isalpha() function in C++. This time, we will be using a string type value within the “isalpha()” function to count the alphabets. Before that, we need to include the “cstring” library and the iostream and cctype library. After adding the namespace and main() function initialization, we have initialized an array string “A” having mixed values, i.e., space, integers, alphabetic, special characters, etc. An integer counter variable “c” is initialized to 0. The “for” loop has been used to iterate each character of string A up to its length. The cout statement will display the value to be checked on each iteration, and the isalpha() function will be used. It will be utilized similarly as we used in the third example to check whether the current character is an alphabet or not. The “if” statement is also using the “isalpha” function to count the total alphabets while using the counter “c”. In the end, a total number of alphabets will be displayed.
After the compilation, the execution of this C++ code results in the following output. It shows the Boolean value for each character within the string “A” separately, i.e., alphabet or not. Also, it shows the total count of alphabets in the string “A”, i.e., 3:
This article was all about checking whether the value mentioned in the isalpha() function was an alphabet or not. We have used the isalpha() function to verify whether the single character variable that is added in the program is an alphabet or not. Also, we have used it to see how many characters in a whole string variable are alphabets. We have tried to balance the level of difficulty in our examples and hope that it will be helpful for our new Linux and C++ users. Check the other Linux Hint articles for more tips and tutorials.