当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。