iswalpha() function in C++ STL



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

What is iswalpha()?

The iswalpha() function is used to check whether a given wide character is an alphabetic character. Wide character means the larger set of characters, which includes the characters from all languages.

The iswalpha() function returns true if the input character is a letter, such as from English, Hindi, Chinese, etc. The regular isalpha() function will only work with alphabets of the English language, that is, the characters defined in the 'char' data type. But iswalpha() function works with 'wchar_t' data type, which makes it suitable for multilingual character identification.

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

wchar_t ch = L 'अ'  // A Devanagari character (used in Hindi)
iswalpha(ch) // Return True

Using iswalpha() Function in STL

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

Syntax

int iswalpha(wint_t ch);

Following are the parameters:

  • Parameter: A wide character to be checked.
  • Return: Returns non-zero if the character is alphabetic; otherwise returns zero.

Steps to Use iswalpha() in C++ STL

The following are steps/algorithm to use iswalpha() using C++ STL:

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

C++ Program to Implement iswalpha() using STL

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

#include <iostream>
#include <cwctype>
#include <clocale>
using namespace std;

int main() {
   // Setting support for all languages
   setlocale(LC_ALL, "");
   
   // Trying a Hindi character
   wchar_t ch = L'अ';
   
   if (iswalpha(ch)) {
      wcout << L"The character '" << ch << L"' is alphabetic." << endl;

   } else {
      wcout << L"The character '" << ch << L"' is not alphabetic." << endl;
   }
   
   return 0;
}

The output of the above code will be:

The character अ is alphabetic.

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.

Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-08-12T17:16:47+05:30

231 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements