linux c 01

isalnum

isalnum函数可以判断一个字符是否是字母或者数字。

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

int main(void)
{
    char str[] = "123c@#$GDG[]?]";
    int i;
    for(i = 0; str[i] != 0; i++)
    {
        if(isalnum(str[i]))
        {
            printf("%c is an alphanumeric character\n", str[i]);
        }
    }

    return 0;
}

isalpha

isalpha函数判断一个字符是否为字母

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

int main(void)
{
    char str[] = "123c@#$GDG[]?]";
    int i;
    for(i = 0; str[i] != 0; i++)
    {
        if(isalpha(str[i]))
        {
            printf("%c is an alpha character\n", str[i]);
        }
    }

    return 0;
}

isascii

isascii函数可以判断字符是否为ascii字符,也就是其值是否在0-127之间。

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

int main(void)
{
    int i;
    for(i = 125; i < 130; i++)
    {
        if(isascii(i))
        {
            printf("%d is ascii character:%c\n", i, i);
        }
        else
        {
            printf("%d is not an ascii character.\n", i);
        }
    }

    return 0;
}

iscntrl

iscntrl函数可以判断字符是不是控制字符,也就是其值是否在0-30之间

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

int main(void)
{
    int i;
    for(i = 25; i < 35; i++)
    {
        if(iscntrl(i))
        {
            printf("%d is control character:%c\n", i, i);
        }
        else
        {
            printf("%d is not an control character.\n", i);
        }
    }

    return 0;
}

isdigit

isdigit判断字符是否为数字。

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

int main(void)
{
    char str[] = "123c@#$GDG[]?]";
    int i;
    for(i = 0; str[i] != 0; i++)
    {
        if(isdigit(str[i]))
        {
            printf("%c is an digit character\n", str[i]);
        }
    }

    return 0;
}

isgraph

isgraph判断字符是否可打印,此宏判断空格不可以被打印出。

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

int main(void)
{
    char str[] = "123c ;@#$GDG[]?]";
    int i;
    for(i = 0; str[i] != 0; i++)
    {
        if(isgraph(str[i]))
        {
            printf("%c is an low-case character\n", str[i]);
        }
    }

    return 0;
}

islower

islower判断字符是否为小写字母。

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

int main(void)
{
    char str[] = "123c ;@#$GDG[]?]";
    int i;
    for(i = 0; str[i] != 0; i++)
    {
        if(islower(str[i]))
        {
            printf("%c is an lower-case character\n", str[i]);
        }
    }

    return 0;
}

isprint

isprint判断字符是否可打印,控制字符就是不可以被打印出来的。

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

int main(void)
{
    char str[] = "a5 @;";
    int i;
    for(i = 0; str[i] != 0; i++)
    {
        if(isprint(str[i]))
        {
            printf("%c is an printable character\n", str[i]);
        }
    }
    
    char c = 23;
    if(isprint(c))
    {
        printf("c is printable\n");
    }

    return 0;
}

isspace

isspace判断字符是不是空格。

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

int main(void)
{
    char str[] = "123c ;@#$G  DG[]?]";
    int i;
    for(i = 0; str[i] != 0; i++)
    {
        if(isspace(str[i]))
        {
            printf("str[%d] is a white-space character: %d\n", i, str[i]);
        }
    }

    return 0;
}

ispunct

ispunct判断字符是否是标点符号或者特殊字符。

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

int main(void)
{
    char str[] = "123c ;@#$G  DG[]?]";
    int i;
    for(i = 0; str[i] != 0; i++)
    {
        if(ispunct(str[i]))
        {
            printf("%c\n", str[i]);
        }
    }

    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值