【发布时间】:2022-06-25 16:13:35
【问题描述】:
#include
int main(){
printf("请输入字符:");
【问题讨论】:
-
这能回答你的问题吗? Check if a char is a digit? (in C)
#include
int main(){
printf("请输入字符:");
【问题讨论】:
这可以通过各种方法解决。选择最适合您的:
使用头文件:#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;
}
使用编程逻辑:
#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;
}
如有任何其他疑问,请随时询问。编码快乐!!
【讨论】: