當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C語言 isalnum()用法及代碼示例


用C編程語言編寫的isalnum()函數檢查給定字符是否為字母數字。在ctype.h頭文件中定義的isalnum()函數。

字母數字:一個字母或數字的字符。
用法:

int isalnum(int x);

例子:


Input:1
Output:Entered character is alphanumeric
Input:A
Output:Entered character is alphanumeric
Input:&
Output:Entered character is not alphanumeric
// C code to illustrate isalphanum() 
#include <ctype.h> 
#include <stdio.h> 
  
int main() 
{ 
    char ch = 'a'; 
  
    // checking is it alphanumeric or not? 
    if (isalnum(ch)) 
        printf("\nEntered character is alphanumeric\n"); 
    else
        printf("\nEntered character is not alphanumeric\n"); 
}

輸出:

Entered character is alphanumeric

應用:isalnum()函數用於查找給定句子(或任何輸入)中字母數字的數量。
例:

Input:abc123@
Output:Number of alphanumerics in the given input is:6
Input:a@#
Output:Number of alphanumerics in the given input is:1
Input:...akl567
Output:Number of alphanumerics in the given input is:6
// C code to illustrate isalphanum() 
#include <ctype.h> 
#include <stdio.h> 
  
int ttl_alphanumeric(int i, int counter) 
{ 
    char ch; 
    char a[50] = "www.geeksforgeeks.org"; 
    ch = a[0]; 
  
    // counting of alphanumerics 
    while (ch != '\0') { 
        ch = a[i]; 
        if (isalnum(ch)) 
            counter++; 
  
        i++; 
    } 
  
    // returning total number of alphanumerics 
    // present in given input 
    return (counter); 
} 
  
int main() 
{ 
    int i = 0; 
  
    int counter = 0; 
    counter = ttl_alphanumeric(i, counter); 
  
    printf("\nNumber of alphanumerics in the "
           "given input is:%d", counter); 
    return 0; 
}

輸出:

Number of alphanumerics in the given input is:19


相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 isalnum() function in C Language。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。