“A variety of functions are available in different languages to be used for specific purposes. One of those dynamic functions, we have isspace() function in C language that is specifically designed to find out if the specified variable is a “space” itself or not. Other than that, it can be used to find out if a string variable contains a “space” character in it or not. This article would explain the use of the isspace function in a variety of ways through different C examples.”
Before moving towards the implementation of C codes, we have to make our system up to date. For this, we have been opening Ubuntu’s every green console application named “Terminal” using a shortcut way, i.e., Ctrl+Alt+T. After the purple query area of the terminal was launched, we tried to execute the “update” instruction using the “apt” utility of our Linux system with “sudo” rights.
It might be possible that you have been using the old version of the Ubuntu system, and it might require an upgrade to work properly shortly. Therefore, we have to use the very same “apt” utility to try out the “upgrade” instruction. After adding our Linux password, it started upgrading our system.
While it’s processing, it will show us the amount of space an upgrade would take. If you have no issues, you can continue upgrading by hitting the “y” key on the keyboard.
To get started with the C language, our Ubuntu Linux system must have a C compiler in it to compile its codes. Right now, we are here to show you the installation of the “GCC” C language compiler on our Linux system through the terminal shell. For this, we are again utilizing the “apt” utility of Linux in the query area of our shell within the “install” instruction followed by the keyword “gcc” for a C compiler. After the Linux password demand, we added our passcode for the current account and continued the process of installation. Within a few seconds, the GCC compiler of C language was installed on our Linux system.
Example 01
Starting our first illustration by creating a “C” file within the current home location via the simple “touch” query. The “ls” instruction shows the successful creation.
The C code has been initiated with the main C headers required to use the standard input, output, and character types in the code. The code contains a single main() function that has been initializing and declaring a character type variable named “val” with a value “space” in its single inverted commas.
After this, an integer variable “res” is initialized that has been taking a value from the isspace() function of C. The isspace function of C uses the character variable “val” in its parameter to check whether it contains a “space” as a value or not. On containing a space, it will return other than 0 integer value to variable “res” otherwise will be returning “0”. As in our case, the value of variable “val” is “space”, so it will definitely return other than 0 to variable “res”. The printf() function statement is here, taking a variable “res” to display the returned value on the console. Our program is completed with the use of a return 0 statement.
#include <ctype.h>
int main() {
char val = ' ';
int res = isspace(val);
printf("Character val is space? : %d\n", res);
return 0;
}
Here comes the use of “gcc” to compile the “isspace.c” file and then execute it using the “./a.out” query. The output of this C example displayed the integer “8192” on the output area, demonstrating that the value in a variable “val” is a “space” character.
If you replace space with some alphabetical character “M”, it will display “0” in return.
#include <ctype.h>
int main() {
char val = 'M';
int res = isspace(val);
printf("Character val is space? : %d\n", res);
return 0;
}
The output displays that the variable “val” is not a space.
Example 02
To be more clear and specific, we will be using the scanf() function to get the input from a user and pass it to the “isspace” function as a variable. Therefore, we have declared the character variable “val,” and a scanf() function is utilized to get input from a user and save it to the variable “val”. The isspace() function checks if the val variable is space or not and saves the result to the “Res” integer variable for further use. The if-else statement is utilized here so far to display the output result accordingly, i.e., If the input added by a user is a space or not. The “if” part would validate if the “Res” variable has value “0” and display that the “val” variable is space. Otherwise, the else statement would display that the variable value is a space.
#include <ctype.h>
int main() {
char val;
printf(HY! Add some value: ");
scanf("%[^\n]c", &val);
int res = isspace(val);
printf("Character val is space? : ");
if(res == 0) {
printf("NO \n"); }
else {
printf("YES \n"); }
return 0;
}
After saving the code via the “nano” editor, we have tried the “gcc” instruction on the shell along with the name of a C file to compile its code. On its first execution after the compilation, we inputted “Hello,” and it returned “NO” as an answer to “Character val is space?”. On adding “M” as input, we got “NO” again, and adding “space” as value returns “YES,” i.e., input is space.
Example 03
Within our last illustration, we will be using the isspace() C function to count the total number of spaces found in an array of characters. For this, we need to use the “string.h” header and initialize a count variable to “0”. A character array gets initialized, and the “For” loop has been utilized to iterate the character array and use each character in the “isspace” function to find out if that is a “space” or not. If a character is a space, it will increment the “count” variable by 1 and display the total count in the last.
#include <ctype.h>
#include <string.h>
int main() {
int count = 0;
char Arr[] = "You have to count spaces here. Now, smile!";
for(int j=0; j<strlen(Arr); j++) {
char c = Arr[j];
if(isspace(c)) {
printf("Value at Index %d is space! : \n", j);
count = count+1;
}}
printf("Total spaces in Array: %d\n", count);
return 0;
}
Each index number containing “Space” along with the total count of spaces in an array is displayed.
Conclusion
At the end of this guide, you will be able to use the isspace() function in your C codes in many different ways without getting any problems. To get hands-on experience in the C language, make sure to practice the above-used programs more and more in different modified ways.