C语言标准库(1)—#include<ctype.h>
来源:互联网 发布:云平台用什么数据库 编辑:程序博客网 时间:2024/05/06 15:29
C语言标准库—#include<ctype.h>
2014/11/25 by jxlijunhao
在这个头文件中包含了对字符测试的函数,如检查一个字符串中某一个字符是否为数字,或者字母等,还包含了字符映射函数,如将大写字母映射成小写字母等。下面是通过查阅标准手册,记录的一些函数的用法,方便以后查找之用。
1,isalpha, 判断是否为字母
isdigit, 判断是否为数字
#include<ctype.h>#include<stdio.h>int main(){int i=0;char str[]="c++11";while (str[i]){if (isalpha(str[i]))printf("character %c is alpha\n",str[i]);else if (isdigit(str[i]))printf("character %c is digit\n",str[i]);else printf("character %c is not alpha or digit\n",str[i]);i++;}}输出结果为:
character c is alphacharacter + is not alphacharacter + is not alphacharacter 1 is digitcharacter 1 is digit
isxdigit :判断是否为 A~F, a~f
2,isalnum: 是字母或者数字
#include<ctype.h>#include<stdio.h>int main(){int i=0;int count=0;char str[]="c++11";//统计一个字符串中是字母或者数字的个数while (str[i]){if (isalnum(str[i])) count++;i++;}printf("there are %d alpha or digit is %s",count,str);return 0;}输出结果:
3
3,islower, 是否为小写,
isupper, 是否为大写
tolower, 转化为小写
touuper 转化为大写
#include<ctype.h>#include<stdio.h>int main(){char str[]="I love ShangHai";//将字符串中的小写字母转化为大写字母int i=0;char c;while (str[i]){c=str[i];if (islower(c))str[i]=toupper(c);i++;}printf("%s\n",str);}
I LOVE SHANGHAI
4, isspace: 判断一个字符是否为空格
#include <stdio.h>#include <ctype.h>int main (){ char c; int i=0; char str[]="Example sentence to test isspace\n"; while (str[i]) { c=str[i]; if (isspace(c)) c='\n'; putchar (c); i++; } return 0;输出为:
Examplesentencetotestisspace
一个应用:将一个包含0~9,A~F的字符串(十六进制)转化成整型:
#include <stdio.h>#include <string.h>#include <ctype.h>long atox(char *s){char xdigs[]="0123456789ABCDEF";long sum;while (isspace(*s))++s;for (sum=0L;isxdigit(*s);++s){int digit=strchr(xdigs,toupper(*s))-xdigs; //地址相减sum=sum*16+digit;}return sum;}int main (){char *str="21";long l=atox(str);printf("%d\n",l);return 0;}
函数strchr ,是包含中<string.h>中的一个函数,其原型为
const char * strchr ( const char * str, int character );返回的是要查找的字符在字符串中第一次出现的位置,返回值是指向该字符的指针
0 0
- C语言标准库(1)—#include<ctype.h>
- <ctype.h> C语言标准库
- C语言专题——标准库<ctype.h>
- C语言ctype.h库
- 《C标准库》——之<ctype.h>
- C /C++标准库 - <cctype> (ctype.h)
- C标准库之二:ctype.h
- C语言--ctype.h
- C语言库函数——ctype.h
- C语言标准库概览详述[2]-字符判断测试<ctype.h>
- C/C++: C标准库 ctype.h 的使用
- C语言标准库(2)--#include<stdio.h>
- C语言标准库(3)--#include<stdlib.h>
- [学习标准库]ctype.h
- linux标准库#include <unistd.h>与windows的#include <windows.h>(C语言开发)
- linux标准库#include <unistd.h>与windows的#include <windows.h>(C语言开发)
- C标准库参考指南(2)ctype.h
- ANSI C标准库: <ctype.h>下函数讲解
- Java版DBHelper【简单】(MySQL数据库) -----超级详细
- uva102
- 求最长公共子串
- iOS多线程的GCD研究
- 第四章 进程描述 4.1——4.3
- C语言标准库(1)—#include<ctype.h>
- ASP.NET2.0雷霆之怒盗链者的祝福
- 我的小记_初学python之装饰器的实现
- ubuntu系统中查看和关闭端口
- 关于struts2 sort标签的使用问题
- java实例之接收用户控制台输入
- 【JS学习笔记】05 JS HTML DOM
- 为什么硅谷公司对Android不感冒?
- Android TextView