C语言题目分享5

文章提供两种C语言编程方法,用于判断用户输入的字符是数字、英文字母、空格还是其他字符。第一种方法使用if-else语句进行判断,第二种方法则结合switch语句实现。程序示例中展示了不同输入下的输出结果。
摘要由CSDN通过智能技术生成

从键盘任意输入一个字符,编程判断该字符是数字字符、英文字母、空格还是其他字符。

**输入格式要求:提示信息:"Press akey and then press Enter:"

**输出格式要求:"It is anEnglish character!\n" "It is a digit character!\n" "It is a space character!\n" "It is other character!\n"

程序运行示例1如下:

Press a key andthen press Enter:A

It is an Englishcharacter!

程序运行示例2如下:

Press a key andthen press Enter:2

It is a digitcharacter!

程序运行示例3如下:

Press a key andthen press Enter:

It is a spacecharacter!

程序运行示例4如下:

Press a key andthen press Enter:#

It is othercharacter!

方法一:
#include <stdio.h>
void main()
{
    char ch;
    ch=getchar();
    if(ch >= 'A' && ch <= 'Z' )
    {
        printf("为大写字符\n");
    }
    else if(ch >= 'a' && ch <= 'z' )
    {
        printf("为小写字符\n");
    }
    else if(ch==' ')
    {
        printf("为空格字符\n");
    }
    else 
    {
        printf("为其他字符\n");
    }
}
方法二:
#include <stdio.h>
void main()
{
    char ch;
    int a;
    ch = getchar();
    if (ch >= 'a' && ch <= 'z')
    ch = '1';
    if (ch >= 'A' && ch <= 'Z')
    ch = '2';
    if( ch == ' ')
        ch = '3';
    switch(ch)
    {
    
    case '1' : printf("xiao \n");break;
    case '2' : printf("da  \n");break;
    case '3' : printf("空格 \n");break;
    default: printf("qita");
    
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fanfan.V

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值