Start your first example by opening a console shell and creating a new file. The terminal should be opened by “Ctrl+Alt+T” or by the activity menu area where all the applications of a Linux system can be found. When the terminal is launched on your screen, try to create a new C++ file using “touch” instruction as already shown in the snap attached below. To open and add a code in it, you can navigate towards the home folder with the help of a file explorer from the desktop. We prefer to open it through the shell using some editor, i.e., vim or nano. So, we choose to open it with the GNU Nano editor with the help of a command shown in the snap.
You will see that your newly created C++ file will be opened in the GNU Nano editor, and it will be empty at first. Add the required standard libraries to use the integer, character type variables, and isalnum() method. So, we have added the standard input-output library header. The “ctype” header is specifically used here for the “isalnum()” method. We have initialized a main() method with the “int” return type.
At the start of the main() method, we have declared an integer variable “x” with value 1, and the character type string variable “Arr” has a string value in it. The variable “x” will be used as an index of an array. The while loop has been initialized to check the condition specified within it. The condition uses the “isalnum()” method to count the number of characters while the character at particular index “x” is alpha-numeric. It will continue to increment the index value until some non-alphanumeric value meets. The printf statement has been used here to tell us how many alpha-numeric characters are there in the character type string variable “Arr” using the value of variable “x”. With the use of the “return 0” clause, the main() method closes. Let’s save your C++ isalnum() code from compiling it on the shell. Use “Ctrl+S” for this and “Ctrl+X” to navigate back towards the terminal console.
If you don’t know about the C++ compilers supported in the Linux distributions, then make sure to install the g++ compiler as per the recommendation. Use the “g++” instruction in the shell using a file name to compile the C++ code. If the compilation command doesn’t return anything, it means the code is semantically correct, and there are no syntax errors. Begin the execution of your file after the compilation with the everlasting “./a.out” command within the console shell.
You can see the output clearly that the character type string holds the first 11 alphanumeric characters. This isalnum() function is taking “@” as a non-alphanumeric character. Therefore, it stops executing further upon finding it.
Let’s change our code a little by opening this file once more. There is no need to change the whole code. We are only going to change the character type string variable “Arr”. So, we have updated its value by “#./” at the start. We have saved this file and closed it after that.
To apply the changes made just now, we have to compile the code again. So, the same “g++” compilation query is utilized. No errors have been found so far. After executing this updated code, we have got the results as displayed on the image attached below. The result shows that we have only 1 alpha-numeric character. This means, when you add more than 1 non-alphanumeric character within the start of a string, it will take the first character as alphanumeric.
Let’s update the code again to see the possible outcomes of taking alphanumeric characters in the string. So, open the file in GNU nano editor again. Update the value of the character type string array “Arr” by adding “’;” at the start of it and save the update. Quit compiling the code.
Now, compile this updated file again using the “g++” compiler command on the console application. After execution, it shows that only the first character is alphanumeric.
This time we will see how the isalnum() method reacts to the alphabetical characters and special symbols. So, we have launched the same file “isalnum.cc” in the editor and changed the value of character type string array “Arr” to “aqsa….”. Now, it has 4 alphabets and 4 dots.
After compiling and running the updated code file, we have 4 as a result, as expected.
Within this instance, we will be checking the isalnum() method for space value. So, we have launched the same file and updated the char type string variable “Arr” value to some spaces. After that, the code is saved, and the file has been quitted.
When we compiled and executed the code again, it returned 1 as its result. It is the same as we have got in example 2 that isalnum() will recognize space as an alphanumeric value.
The last example is quite different from the rest of the examples discussed above. We have updated the whole code of a C++ file “isalnum.cc” this time. Added the input-output stream header file with the word “include” at the first line. The standard namespace has been utilized to make our code error-free. The main() function is started with initializing a character type array variable “Arr” having a size “200”. This means we can add up to 200 characters to it. The “cout” clause is used here to ask a user to add some input. The “cin.get” input stream will allow the compiler to add the value in an “Arr” variable while getting the value from the user up to size 200.
The “for” loop has been used here to iterate the values inputted by a user in the array “Arr” and check if the value is not equal to “\0”. Until then, it will continue to iterate. The “if” statement utilizes the “isalnum()” method to check if the character in an array is not alphanumeric and display it on the shell with the “cout” command.
Compile this newly built code. After executing the code three times, we have inputted different values and got different results each time.
This tutorial guide covers the “isalnum()” method to check if the characters are alphanumeric or not. To achieve our goal, we have used integer type variables and character type string arrays. Moreover, the “if” statements, “for” and “while” loops were also very helpful to bring out the required results. We hope this article will be valuable for everyone.