Open In App

iswxdigit() function in C/C++

Last Updated : 30 Aug, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The iswxdigit() is a built-in function in C/C++ which checks if the given wide character is a hexadecimal digit character or not. It is defined within the cwctype header file of C++. The available hexadecimal numeric characters are:

  • Digits (0 to 9)
  • Lowercase alphabets from a to f
  • Uppercase alphabets from A to F

Syntax:

int iswxdigit(ch)

Parameter: The function accepts a single mandatory parameter ch which specifies the wide character which we have to check that if it is a hexadecimal or not.

Return Value: The function returns two values as shown below.

  • If the ch is a hexa decimal, then a non-zero value is returned.
  • If it is not a hexa decimal then 0 is returned.

Below programs illustrates the above function.

Program 1:




// C++ program to illustrate
// iswxdigit() function
#include <cwchar>
#include <cwctype>
#include <iostream>
using namespace std;
  
// function to check if
// the wide character is hexadecimal or not
void ishexadecimal(wchar_t* str)
{
    bool flag = false;
    for (int i = 0; i < wcslen(str); i++) {
        if (!iswxdigit(str[i])) {
            flag = true;
            break;
        }
    }
  
    if (flag)
        wcout << str << L" is not a valid"
              << " hexadecimal number" << endl;
    else
        wcout << str << L" is a valid"
              << " hexadecimal number" << endl;
}
  
// Driver Code
int main()
{
    wchar_t str[] = L"a3lz";
    ishexadecimal(str);
  
    wchar_t str1[] = L"10dbe";
    ishexadecimal(str1);
  
    return 0;
}


Output:

a3lz is not a valid hexadecimal number
10dbe is a valid hexadecimal number

Program 2:




// C++ program to illustrate
// iswxdigit() function
#include <cwchar>
#include <cwctype>
#include <iostream>
using namespace std;
  
// function to check if
// the wide character is hexadecimal or not
void ishexadecimal(wchar_t* str)
{
    bool flag = false;
    for (int i = 0; i < wcslen(str); i++) {
        if (!iswxdigit(str[i])) {
            flag = true;
            break;
        }
    }
  
    if (flag)
        wcout << str << L" is not a valid"
              << " hexadecimal number" << endl;
    else
        wcout << str << L" is a valid"
              << " hexadecimal number" << endl;
}
  
// Driver Code
int main()
{
    wchar_t str[] = L"1441a";
    ishexadecimal(str);
  
    wchar_t str1[] = L"xyz2";
    ishexadecimal(str1);
  
    return 0;
}


Output:

1441a is a valid hexadecimal number
xyz2 is not a valid hexadecimal number

Similar Functions : isalpha() and isdigit() functions in C/C++ with example



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads