C library - iswalnum() function



The C wctype library iswalnum() function is used to check whether a given wide character (represented by wint_t) is an alphanumeric character, i.e. either an alphabet or a digit specific to the current locale.

This function can be useful for character validation, password validation, string processing, or tokenization.

Syntax

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

int iswalnum( 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 an alphanumeric character, zero otherwise.

Example 1

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

#include <wctype.h>
#include <stdio.h>
int main() {
   wint_t ch = L'5'; 

   if (iswalnum(ch)) {
      printf("The wide character %lc is alphanumeric.\n", ch);
   } else {
      printf("The wide character %lc is not alphanumeric.\n", ch);
   }
   return 0;
}

Output

Following is the output −

The wide character 5 is alphanumeric.

Example 2

We create a c program to count the number of alphanumeric letter using the iswalnum().

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

int main() {
   // Define a wide string with mixed characters
   wchar_t str[] = L"Tutorialspoint Inida 500081";
   int alnumCount = 0;
   
   // Iterate over each character in the wide string
   for (int i = 0; str[i] != L'\0'; i++) {
      if (iswalnum(str[i])) {
         wprintf(L"%lc ", str[i]);
         alnumCount++;
      }
   }
   
   // Print the result
   wprintf(L"\nThe wide string \"%ls\" contains %d alphanumeric character(s).\n", str, alnumCount);
   
   return 0;
}

Output

Following is the output −

T u t o r i a l s p o i n t I n i d a 5 0 0 0 8 1
The wide string "Tutorialspoint Inida 500081" contains 25 alphanumeric character(s).

Example 3

Here, we are checking all the letters of wide character, whether the letter is alphanumeric or not.

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

int main() {
   wchar_t str[] = L"ab518";   
   for (int i = 0; str[i] != L'\0'; i++) {
       if (iswalnum(str[i])) {
           wprintf(L"The character '%lc' is an alphanumeric character.\n", str[i]);
       } else {
           wprintf(L"The character '%lc' is not an alphanumeric character.\n", str[i]);
       }
   }   
   return 0;
}

Output

Following is the output −

The character 'a' is an alphanumeric character.
The character 'b' is an alphanumeric character.
The character '5' is an alphanumeric character.
The character '1' is an alphanumeric character.
The character '8' is an alphanumeric character.
c_library_wctype_h.htm
Advertisements