【问题标题】:Write a c program to check if given character is digit or not编写一个c程序来检查给定的字符是否为数字
【发布时间】:2022-06-25 16:13:35
【问题描述】:

#include

int main(){

printf("请输入字符:");

【问题讨论】:

标签: c character digits


【解决方案1】:

这可以通过各种方法解决。选择最适合您的:

  1. 使用头文件:#include <ctype.h>

    // C program to demonstrate isdigit()
        #include <ctype.h>
        #include <stdio.h>
    
    // Driver Code
        int main()
       {
    // Taking input
      char ch = '7';
    
    // Check if the given input
    // is numeric or not
       if (isdigit(ch))
       printf("\nEntered character is"
         " numeric character");
      else
       printf("\nEntered character is not"
         " a numeric character");
      return 0;
    }
    
  2. 使用编程逻辑:

    #include<stdio.h>
    #include<conio.h>
    
    int main()
    {
      char ch;
      clrscr();
      printf("Enter chracter: ");
      scanf("%c", &ch);
    
      if(ch>='0' && ch<='9')
      {
       printf("%c is DIGIT.", ch);
       }
       else
       {
        printf("%c is NOT DIGIT.", ch);
        }
    
       getch();
       return(0);
       }
    

或者更描述性:

#include <stdio.h>

int main()
{
char ch;

/* Input character from user */
printf("Enter any character: ");
scanf("%c", &ch);


/* Alphabet check */
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
    printf("'%c' is alphabet.", ch);
}
else if(ch >= '0' && ch <= '9')
{
    printf("'%c' is digit.", ch);
}
else 
{
    printf("'%c' is special character.", ch);
}

return 0; 
}

如有任何其他疑问,请随时询问。编码快乐!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多