In this guide, we will discuss the syntax, functionality, and output of the C++ iswspace function.
What is the iswspace function?
The C++ standard library's iswspace function is declared in the <cwctype> header file. This function checks if the given wide character is a whitespace character, behaving similarly to the isspace function but for wide characters. When the input character is a whitespace character, the function evaluates it as true and outputs a non-zero integer; otherwise, it returns zero to indicate false.
Syntax:
It has the following syntax:
int iswspace(wint_t ch);
Parameters:
The function requires a single mandatory parameter, ch, representing the wide character to be examined for the presence of a wide whitespace character. This argument is converted into either WEOF or wintt. The wintt type is used to hold integral data.
Value returned
If the function results in a false outcome, it will yield a value of zero; conversely, when it returns true, it will provide any non-zero value.
The preceding function is illustrated by the programs following.
Program 1:
// C++ program to illustrate
// iswspace() function
#include <cwctype>
#include <iostream>
using namespace std;
int main()
{
wchar_t c;
int i = 0;
wchar_t str[] = L"Welcome to Cpp Tutorial - Learn C++ Programming\n";
// Check for every character
// in the string
while (str[i]) {
c = str[i];
// Function to check if the character
// is a wide whitespace or not
if (iswspace(c))
c = L'\n';
putwchar(c);
i++;
}
return 0;
}
Output:
Welcome
to
Cpp Tutorial
-
Learn
C++
Programming
Program 2:
// C++ program to illustrate
// iswspace() function
#include <cwctype>
#include <iostream>
using namespace std;
int main()
{
wchar_t c;
int i = 0;
wchar_t str[] = L"This is a small example.\n";
// Check for every character
// in the string
while (str[i]) {
c = str[i];
// Function to check if the character
// is a wide whitespace or not
if (iswspace(c))
c = L'\n';
putwchar(c);
i++;
}
return 0;
}
Output:
This
is
a
small
example.
Conclusion:
In summary, determining if a wide character is a white-space character can be achieved effortlessly by employing the iswspace function in C/C++. When dealing with extensive character sets, programmers can enhance the adaptability and compatibility of their code by leveraging this technique from the <cwctype> header.