c语言:22、字符函数(ctype、stdlib)

1、ctype.h

ctype.h头文件中定义了一系列对字符的数据类型的判断
更多函数请参考:http://c.biancheng.net/ref/isalnum.html

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

#define PRINTF(format, ...) printf("("__FILE__":%d) %s: "format,__LINE__,__FUNCTION__, ##__VA_ARGS__)
#define PRINT_INT(value) PRINTF(#value":%d \n", value)
#define PRINT_CHAR(value) PRINTF(#value":%c \n", value)

int main(){
    PRINT_INT(isalnum('A'));//判断字符是否为字母或数字a-zA-Z,0-9均返回非0
    PRINT_INT(isalpha('A'));//判断是否是字母a-zA-Z均为字母,返回非0
    PRINT_INT(isblank(' '));//判断是否为空字符
    PRINT_INT(iscntrl('\t'));//判断是否为控制字符
    PRINT_INT(isdigit('a'));//判断是否为一个十进制数
    PRINT_CHAR(tolower('A'));//字符转小写
    PRINT_CHAR(toupper('a'));//字符转大写
    return 0;
}
#ifndef stdio_h
#include <stdio.h>
#endif

#ifndef ctype_h
#include <ctype.h>
#endif

#define PRINTLN(format) printf("%s  %d:"format"\n",__FUNCTION__,__LINE__)
#define PRINTFLN(format, ...) printf(#format":"format"\n", ##__VA_ARGS__)

int main(){
    PRINTLN("检测参数字符是否为数字或字母");
    printf("%d\n", isalnum('a'));//2
    printf("%d\n", isalnum('B'));//1
    printf("%d\n", isalnum('2'));//4
    printf("%d\n", isalnum('-'));//0
    
    PRINTLN("检测参数字符是否为字母");
    printf("%d\n", isalpha('2'));//0
    printf("%d\n", isalpha('a'));//2
    printf("%d\n", isalpha('A'));//1

    PRINTLN("检测参数字符是否为十进制数字");
    printf("%d\n",isdigit('a'));//0
    printf("%d\n",isdigit('1'));//4

    PRINTLN("检测参数字符是否为十六进制数字");
    printf("%d\n",isxdigit('1'));//128
    printf("%d\n",isxdigit('A'));//128
    printf("%d\n",isxdigit('K'));//0

    PRINTLN("检测所传字符是否为小写字母");
    printf("%d\n",islower('a'));//2
    printf("%d\n",islower('B'));//0
    printf("%d\n",islower('2'));//0

    PRINTLN("检测所传字符是否为大写字母");
    printf("%d\n",isupper('A'));//1
    printf("%d\n",isupper('a'));//0
    printf("%d\n",isupper('2'));//0

    PRINTLN("检测参数是否为标点符号");
    printf("%d\n",ispunct('c'));//0
    printf("%d\n",ispunct('2'));//0
    printf("%d\n",ispunct('A'));//0
    printf("%d\n",ispunct('.'));//16

    PRINTLN("检测参数是否为空白字符");
    printf("%d\n",isspace(' '));//8
    printf("%d\n",isspace(' '));//8
    printf("%d\n",isspace('a'));//0
    return 0;
}

2、stdlib.h

stdlib.h头文件中定义了一些变量和宏以及函数。
详细可参考:这里

2.1、atoX系列函数

其中atoX(const char *str)系列函数拥有将字符串转换类型的功能。

#ifndef stdio_h
#include <stdio.h>
#endif
#ifndef stdlib_h
#include <stdlib.h>
#endif
#define PRINTF(format, ...) printf(format, ##__VA_ARGS__)
#define PRINTLN(value) PRINTF(value"\n")
#define PRINTF_F(value) PRINTF(#value": %f\n", value)
#define PRINTF_I(value) PRINTF(#value": %i\n", value)

int main(){
    PRINTLN("把参数字符串转为一个浮点数(类型为double)");
    PRINTF_F(atof("12.34"));//结果为12.34
    PRINTF_F(atof("-12e34"));//结果为:-12e34(支持科学计数法)
    PRINTF_F(atof("      1.234abcd"));//结果为:1.234
    PRINTF_F(atof("0x10"));//结果为0(不同编辑器结果不同,部分编辑器支持16进制)
    PRINTF_F(atof("0x10p3"));//结果为0(不同编辑器结果不同,部分编辑器支持16进制)

    PRINTLN("\n\n把参数字符串转为一个整数");
    PRINTF_I(atoi("1234"));//结果为:1234
    PRINTF_I(atoi("-1234"));//结果为:-1234
    PRINTF_I(atoi("         1234abc"));//结果为1234
    PRINTF_I(atoi("0x10"));//结果为0(不支持十六进制数)
    PRINTF_I(atoi("a"));//结果为0

    PRINTLN("把参数转为long int");
    PRINTF_I(atol("3"));
    return 0;
}

上方代码调用结果如下
在这里插入图片描述

2.2、strtoX系列函数

strtoX(const char *str, char **endptr)相关的函数功能和atoX系列函数功能差不多,但是可重复解析,更安全,更强大

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值