iswalnum Function in C++ STL



The iswalnum() function is an extension of isalnum() function to support character identification of all languages. In this article, we will learn how to use the iswalnum() function from the Standard Template Library (STL) in C++.

What is iswalnum()?

The iswalnum() function is used to check whether a given wide character is alphanumeric. An alphanumeric character is either an alphabet letter (a-z, A-Z) or a digit (0-9). Wide character means, the larger set of characters, which include the characters from all the languages. The iswalnum() function returns true if the input character is either a letter from any language or a digit (0-9). The regular isalnum() function will only work with alphabets of english language, that is the characters defined in 'char' data type. But iswalnum() function works with 'wchar_t' data type, which makes it suitable for multilingual character identification.

For example, in the code we have shown how iswalnum() checks characters:

wchar_t ch = L'?'; // A Devanagari character (used in Hindi) iswalnum(ch) // Return True

Using iswalnum() Function in STL

The iswalnum() function is defined in the <cwctype> header of STL. It checks if a wide character is either a letter or digit. Below are some points about this function:

  • Header: <cwctype>
  • Syntax:
    int iswalnum(wint_t ch);
  • Parameter: A wide character to be checked.
  • Return: Returns non-zero if the character is alphanumeric; otherwise returns zero.

Steps to Use iswalnum() in C++ STL

Following are steps/algorithm to use iswalnum() using C++ STL:

  • Include the <cwctype> header file.
  • Declare a wide character using wchar_t.
  • Pass the character to the iswalnum() function.
  • Use conditional logic to interpret the result.

C++ Program to Implement iswalnum() using STL

The below code is the implementation of the above algorithm in C++ language.

Open Compiler
#include <iostream> #include <iostream> #include <cwctype> #include <clocale> using namespace std; int main() { // Setting support for all language setlocale(LC_ALL, ""); // Trying Hindi character wchar_t ch = L'?'; if (iswalnum(ch)) { wcout << L"The character '" << ch << L"' is alphanumeric." << endl; } else { wcout << L"The character '" << ch << L"' is not alphanumeric." << endl; } return 0; }

The output of above code will be:

The character '?' is alphanumeric.

Time and Space Complexity

Time Complexity: O(1), as it is a simple character check.

Space Complexity: O(1), as it uses a constant amount of space.

Updated on: 2025-05-13T19:31:26+05:30

200 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements