C library - iswspace() function



The C wctype library iswspace() function is used to check whether a given wide character (of type wint_t) is a whitespace character or not.

Whitespace character includes spaces, tabs, newlines, and other characters that are considered whitespace in various locales, Specifically, these characters are:

  • Space (0x20)
  • Form feed (0x0c)
  • Line feed (0x0a)
  • Carriage return (0x0d)
  • Horizontal tab (0x09)
  • Vertical tab (0x0b)

Syntax

Following is the C library syntax of iswspace() function −

int iswspace( wint_t ch )

Parameters

This function accepts a single parameter −

  • ch − It is a wide character of type 'wint_t' to be checked.

Return Value

This function returns Non-zero value if the wide character is a whitespace character, zero otherwise.

Example 1

The following is the basic c example that demonstrate the use of iswspace() function.

#include <stdio.h>
#include <wchar.h>
#include <wctype.h>

int main(void) {
   wchar_t ch = L' ';
   
   // use ternary operator 
   int is_whitespace = iswspace(ch) ? 1 : 0; 

   printf("Character '%lc' is%s whitespace character\n", ch, is_whitespace ? "" : " not");

   return 0;
}

Output

Following is the output −

Character ' ' is whitespace character

Example 2

Let's create another c program and use iswspace() to validate whether each element of the array is whitespace or not.

#include <locale.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>

int main(void) {
    // Set the locale to "en_US.utf8"
    setlocale(LC_ALL, "en_US.utf8");

    // Array of wide characters 
    wchar_t wchar[] = {
        L'X', L' ', L'\n', L'\t', L'@', L'\u00A0', L'\u2003'
    };
    size_t size = sizeof(wchar) / sizeof(wchar[0]);

    printf("Checking whitespace characters:\n");
    for (size_t i = 0; i < size; ++i) {
        wchar_t ch = wchar[i];
        printf("Character '%lc' (U+%04X) is %s whitespace character\n", ch, ch, iswspace(ch) ? "a" : "not a");
    }
    return 0;
}

Output

Following is the output −

Checking whitespace characters:
Character 'X' (U+0058) is not a whitespace character
Character ' ' (U+0020) is a whitespace character
Character '
' (U+000A) is a whitespace character
Character ' ' (U+0009) is a whitespace character
Character '@' (U+0040) is not a whitespace character
Character ' ' (U+00A0) is not a whitespace character
Character ' ' (U+2003) is a whitespace character

Example 3

The below c example uses the iswspace() function to break a statement after encountering whitespace.

#include <stdio.h>
#include <wchar.h>
#include <wctype.h>

int main(void) {
   // wide char array
   wchar_t statement[] = L"Tutrialspoint India pvt ltd";
   // find the length of wide char
   size_t len = wcslen(statement);

   for (size_t i = 0; i < len; ++i) {
      // Check if the current character is whitespace
      if (iswspace(statement[i])) {
         // Print a newline character if whitespace is found
         putchar('\n');
      } else {
         // Print the current character if it's not whitespace
         putchar(statement[i]);
      }
   }
   return 0;
}

Output

Following is the output −

Tutrialspoint
India
pvt
ltd
c_library_wctype_h.htm
Advertisements