Isspace function C++

You may have used many variables and functions while coding. Have you ever tried to use some functions to find out the total number of spaces used in between some variable values? If not, then you are extremely welcome in today’s tutorial guide.

This article is all about the use of the isspace() function of the C++ language to find out if the specified value is space or not.

Let’s make a fresh start to our article by producing a C++ file.

We need Ubuntu’s shell to create a C++ document in Ubuntu’s home folder. To do that, open Ubuntu’s shell using “Ctrl+Alt+T” and use the “touch” instruction as shown below.

Your new C++ document will be created in a blink.

The newly made file is successfully launched within Ubuntu’s shell “nano” editor. We are using the system’s nano editor to open our new C++ file to create code.

Example 01:

Let’s get started with the most basic example of a C++ program using the “space” function.

We use the “iostream” header that is essential for the use of regular input and output streams.

After that, we will use the “#include” keyword for the inclusion of the “cctype” library at the C++ code start. Without the use of the “cctype” library, the function “isspace” will be of no use in the code and the code won’t work the way we want.

As you know, the C++ code starts with the main() function. So, we are using the only main() function implementation without using any user-defined function after the headers.

We start the main() function implementation with the character variable “v” initialization to space. After that, the integer variable “n” is initialized with the “isspace()” function taking variable “c” as its parametric value. The function “isspace” will check whether the variable “v” is itself a space or not. Save the resultant returned value by issapce() to the integer variable “n”.

At the next line, the c++ standard “cout” statement is cast-off to display the result of variable “n” (i.e. if it is a space or not).

Now, the main function implementation is completed. The program is now complete and ready to use.

You cannot use or run the C++ code without compiling it first.

Within Ubuntu 20.04, you must have a g++ compiler of C++ language to compile your codes. Otherwise, your code won’t work.

So, we have to install it first using the “apt” package and use it on the shell as “g++” instruction followed by the c++ filename. This way, our code will be compiled and is error-free as shown below in the g++ instruction output.

Now, it’s time to run the “a./.out” command in the console terminal to execute our already compiled file (i.e. “isspace.cc”). Run the “a.out” instruction as demonstrated in the output below.

It shows “8192” which means the value for variable “v” is a space.

We have seen how isspace() function outputs when we use space as value to a variable. Let’s see how it will turn out when we use other than space as a variable value.

We have launch the same file again in the Gnu nano editor, and updat the code by replacing the space with the “z” alphabet in character variable “v”.

We don’t need to alter a single character in the C++ code.

Now, it’s time to save the C++ code using “Ctrl+S” and exit it with “Ctrl+X”. We try both shortcuts one after another.

Now, use the g++ compiler of the Ubuntu 20.04 system to compile the newly updated code again.

After the compilation works, we execute the updated code. We get the output 0 since the value “z” was not a space according to isspace() function.

Example 02:

We can get a similar output or more accurate output with boolean variables in the code.

We initialize two boolean variables – v1 and v2. Both variables are utilizing the isspace() function to check the two different values in parameters.

The first value for v1 is a space and the other value for v2 is a number. Then, we use the cout statements utilizing the boolean variables v1 and v2 to see the output results. Let’s save this new program first and then use it within the compilation instruction.

The code is displayed in the image below.

Our compilation for this updated code is quite successful with the g++ compiler.

The execution command “./a.out” was used after that. We got 1 for v1 since it contains a space, and 0 for v2 since “7” is not a space value.

Example 03:

Let’s take a look at a new example. This time, we will use a string variable to check within the “isspace()” method.

To use string variables in the C++ code, we have to include the “cstring” header after the iostream and cctype library. After using the standard namespace, we start the main() function. Within a main() function, an integer variable “c” is initialized to 0, which will be used as a counter.

After that, an array string variable “A” will result to a mixed values (i.e. space, integers, alphabetic, special characters, etc). An integer counter variable “c” is initialized with some string value “Test string in c++”.

Now, to check the number of spaces in the “A” string, we must iterate the characters in the string “A” one after another. On each “for” loop iteration, the specific index value is saved to another character variable “v”.

Then, the isspace() function is used within the “if” statement to check whether the value in “v” is space. If the condition is successful, it will display the value on the shell using “cout” and increment the counter “c” by 1.

A total number of spaces is shown.

After executing the updated code, the index numbers for string “A” that contains space are displayed along with the total count of spaces.

Example 04:

There are other characters in C++ that is being used as whitespace. These characters include \n, \r, \f, \t, and \v. We decide to use these characters in a string.

We open the same code file and update the string “A” by adding all these characters after each word. The rest of the C++ code was left untouched.

Let’s execute our code to see what we get after the execution.

It shows all the indexes where we placed the space characters in a string and also shows the total space counts (i.e. 6).


This article is implemented on Ubuntu 20.04 to demonstrate if the use of the isspace() function in C++ is alphabet or not. In our initial examples, we tried to find out whether the specified variable got a space value or not. After that, we also found out the total spaces in specific variables using isspace() and count variable and utilized other characters in a string that are being used as space.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.