C 语言判断字符是字母还是数字的程序

编写一个 C 语言程序,使用内置函数 isalpha、isdigit 以及不使用任何内置函数来检查字符是字母还是数字。

C 语言判断字符是字母还是数字的程序

此程序允许用户输入一个字符。然后它将检查用户指定的字符是字母还是数字。

#include <stdio.h>

int main()
{
  char ch;
  printf(" Please Enter any character :  ");
  scanf("%c", &ch);
  
  if( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') )
  {
  	printf("\n %c is an Alphabet", ch);  	
  }
  else if (ch >= '0' && ch <= '9')
  {
  	printf("\n %c is a Digit", ch);  	
  }    
  else
    printf("\n %c is not an Alphabet, or a Digit", ch);
  
  return 0;
}
C Program to check whether the Character is Alphabet or Digit 1

让我们检查一下 7 是字母还是数字

 Please Enter any character :  7

 7 is a Digit

让我们检查一下 $ 是字母还是不是(不为真)

 Please Enter any character :  $

 $ is not an Alphabet, or a Digit

使用 ASCII 值的 C 语言程序,用于检查字符是字母还是数字

在此程序中,我们使用 ASCII 值来检查输入的字符是字母还是数字。我建议您参考 ASCII 表来理解此 编程中每个字符的值。

#include <stdio.h>

int main()
{
  char ch;
  printf(" Please Enter any character :  ");
  scanf("%c", &ch);
  
  if (ch >= 48 && ch <= 57)
  {
  	printf("\n %c is a Digit", ch);  	
  }
  else if ( (ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) )
  {
  	printf("\n %c is an Alphabet", ch);  	
  }    
  else
    printf("\n %c is not an Alphabet, or a Digit", ch);
  
  return 0;
}
 Please Enter any character :  7

 7 is a Digit

让我们检查一下 # 是字母还是不是(不为真)

 Please Enter any character :  #

 # is not an Alphabet, or a Digit

让我们检查一下 g 是字母还是不是(为真)

 Please Enter any character :  g

 g is an Alphabet

在此 程序中,我们使用内置函数 isalphaisdigit 来检查输入的字符是字母还是数字。

#include <stdio.h>
#include<ctype.h>

int main()
{
  char ch;
  printf(" Please Enter any character :  ");
  scanf("%c", &ch);
  
  if (isdigit(ch))
  {
  	printf("\n %c is a Digit", ch);  	
  }
  else if ( isalpha(ch) )
  {
  	printf("\n %c is an Alphabet", ch);  	
  }    
  else
    printf("\n %c is not an Alphabet, or a Digit", ch);
  
  return 0;
}
 Please Enter any character :  @

 @ is not an Alphabet, or a Digit