iswdigit() function in C++ STL

C++Server Side ProgrammingProgramming

In C++ STL, iswdigit() function is a built-in function that is used to check if the given wide character is a decimal digit character or some other character. This function is present in a cwctype header file in C/C++.

What are the decimal digit characters?

Decimal digit character are the numeric values that starts from 0 i.e. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 .

Syntax of iswcntrl() function is as follows

int iswdigit() (wint_t c)

Parameters − c is a wide character to be checked, casted to a wint_t, or WEOF where wint_t is an integral type.

Return Value − A value different from zero (i.e., true) if indeed c is a decimal digit and value zero (i.e., false) otherwise.

Approach used in the below program is as follows

  • Input the string in a variable let’s say str[] of type string

  • Call the function iswdigit() to check whether the given wide character is a decimal digit or not

  • Print the result

Example-1

 Live Demo

#include <cwctype>
#include <iostream>
using namespace std;
int main(){
   wchar_t c_1 = '2';
   wchar_t c_2 = '*';
   // Function to check if the character
   // is a digit or not
   if (iswdigit(c_1))
      wcout << c_1 << " is a character ";
   else
      wcout << c_1 << " is a digit ";
      wcout << endl;
   if (iswdigit(c_2))
      wcout << c_2 << " is a character ";
   else
      wcout << c_2 << " is a digit ";
   return 0;
}

Output

If we run the above code it will generate the following output −

2 is a digit
* is a character

Example-2

 Live Demo

#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
int main (){
   wchar_t str[] = L"1776ad";
   long int year;
   if (iswdigit(str[0])) {
      year = wcstol (str,NULL,10);
      wprintf (L"The year that followed %ld was %ld.\n",year,year+1);
   }
   return 0;
}

Output

If we run the above code it will generate the following output −

The year 1777 followed 1776
raja
Published on 30-Jan-2020 09:42:22
Advertisements