Linux常用C函数---字符测试篇
函数讲解部分参考http://net.pku.edu.cn/~yhf/linux_c/
| isalnum(测试字符是否为英文或数字) | |
|
相关函数
|
isalpha,isdigit,islower,isupper |
|
表头文件
|
#include<ctype.h> |
|
定义函数
|
int isalnum (int c) |
|
函数说明
|
检查参数c是否为英文字母或阿拉伯数字,在标准c中相当于使用“isalpha(c) || isdigit(c)”做测试。 |
|
返回值
|
若参数c为字母或数字,则返回TRUE,否则返回NULL(0)。 |
|
附加说明
|
此为宏定义,非真正函数。 |
|
范例
|
/* 找出str 字符串中为英文字母或数字的字符*/ #include < ctype.h> main() { char str[]=”123c@#FDsP[e?”; int i; for (i=0;str[i]!=0;i++ ) if ( isalnum(str[i])) printf(“%c is an alphanumeric character\n”,str[i]); } |
|
执行
|
1 is an apphabetic character 2 is an apphabetic character 3 is an apphabetic character c is an apphabetic character F is an apphabetic character D is an apphabetic character s is an apphabetic character P is an apphabetic character e is an apphabetic character |
|
|
|
|
|
isalpha (测试字符是否为英文字母) |
|
相关函数
|
isalnum,islower,isupper |
|
表头文件
|
#include<ctype.h> |
|
定义函数
|
int isalpha (int c) |
|
函数说明
|
检查参数c是否为英文字母,在标准c中相当于使用“isupper(c)||islower(c)”做测试。 |
|
返回值
|
若参数c为英文字母,则返回TRUE,否则返回NULL(0)。 |
|
附加说明
|
此为宏定义,非真正函数。 |
|
范例
|
/* 找出str 字符串中为英文字母的字符*/ #include <ctype.h> main() { char str[]=”123c@#FDsP[e?”; int i; for (i=0;str[i]!=0;i++) if(isalpha(str[i])) printf(“%c is an alphanumeric character\n”,str[i]); } |
|
执行
|
c is an apphabetic character F is an apphabetic character D is an apphabetic character s is an apphabetic character P is an apphabetic character e is an apphabetic character |
|
|
|
|
|
isascii(测试字符是否为ASCII 码字符) |
|
相关函数
|
iscntrl |
|
表头文件
|
#include <ctype.h> |
|
定义函数
|
int isascii(int c); |
|
函数说明
|
检查参数c是否为ASCII码字符,也就是判断c的范围是否在0到127之间。 |
|
返回值
|
若参数c为ASCII码字符,则返回TRUE,否则返回NULL(0)。 |
|
附加说明
|
此为宏定义,非真正函数。 |
|
范例
|
/* 判断int i是否具有对映的ASCII码字符*/ #include<ctype.h> main() { int i; for(i=125;i<130;i++) if(isascii(i)) printf("%d is an ascii character:%c\n",i,i); else printf("%d is not an ascii character\n",i); } |
|
执行
|
125 is an ascii character:} 126 is an ascii character:~ 127 is an ascii character: 128 is not an ascii character 129 is not an ascii character |
|
|
|
|
|
iscntrl(测试字符是否为ASCII 码的控制字符) |
|
相关函数
|
isascii |
|
表头文件
|
#include <ctype.h> |
|
定义函数
|
int iscntrl(int c); |
|
函数说明
|
检查参数c是否为ASCII控制码,也就是判断c的范围是否在0到30之间。 |
|
返回值
|
若参数c为ASCII控制码,则返回TRUE,否则返回NULL(0)。 |
|
附加说明
|
此为宏定义,非真正函数。 |
|
|
|
|
|
isdigit(测试字符是否为阿拉伯数字) |
|
相关函数
|
isxdigit |
|
表头文件
|
#include<ctype.h> |
|
定义函数
|
int isdigit(int c) |
|
函数说明
|
检查参数c是否为阿拉伯数字0到9。 |
|
返回值
|
若参数c为阿拉伯数字,则返回TRUE,否则返回NULL(0)。 |
|
附加说明
|
此为宏定义,非真正函数。 |
|
范例
|
/* 找出str字符串中为阿拉伯数字的字符*/ #include<ctype.h> main() { char str[]="123@#FDsP[e?"; int i; for(i=0;str[i]!=0;i++) if(isdigit(str[i])) printf("%c is an digit character\n",str[i]); } |
|
执行
|
1 is an digit character 2 is an digit character 3 is an digit character |
|
|
|
|
|
isgraphis(测试字符是否为可打印字符) |
|
相关函数
|
isprint |
|
表头文件
|
#include <ctype.h> |
|
定义函数
|
int isgraph (int c) |
|
函数说明
|
检查参数c是否为可打印字符,若c所对映的ASCII码可打印,且非空格字符则返回TRUE。 |
|
返回值
|
若参数c为可打印字符,则返回TRUE,否则返回NULL(0)。 |
|
附加说明
|
此为宏定义,非真正函数。 |
|
范例
|
/* 判断str字符串中哪些为可打印字符*/ #include<ctype.h> main() { char str[]="a5 @;"; int i; for(i=0;str[i]!=0;i++) if(isgraph(str[i])) printf("str[%d] is printable character:%d\n",i,str[i]); } |
|
执行
|
str[0] is printable character:a str[1] is printable character:5 str[3] is printable character:@ str[4] is printable character:; |
|
|
|
|
|
islower(测试字符是否为小写字母) |
|
相关函数
|
isalpha,isupper |
|
表头文件
|
#include<ctype.h> |
|
定义函数
|
int islower(int c) |
|
函数说明
|
检查参数c是否为小写英文字母。 |
|
返回值
|
若参数c为小写英文字母,则返回TRUE,否则返回NULL(0)。 |
|
附加说明
|
此为宏定义,非真正函数。 |
|
范例
|
#include<ctype.h> main() { char str[]="123@#FDsP[e?"; int i; for(i=0;str[i]!=0;i++) if(islower(str[i])) printf("%c is a lower-case character\n",str[i]); } |
|
执行
|
c is a lower-case character s is a lower-case character e is a lower-case character |
|
|
|
|
|
isprint(测试字符是(否为可打印字符) |
|
相关函数
|
isgraph |
|
表头文件
|
#include<ctype.h> |
|
定义函数
|
int isprint(int c); |
|
函数说明
|
检查参数c是否为可打印字符,若c所对映的ASCII码可打印,其中包含空格字符,则返回TRUE。 |
|
返回值
|
若参数c为可打印字符,则返回TRUE,否则返回NULL(0)。 |
|
附加说明
|
此为宏定义,非真正函数。 |
|
范例
|
/* 判断str字符串中哪些为可打印字符包含空格字符*/ #include<ctype.h> main() { char str[]="a5 @;"; int i; for(i=0;str[i]!=0;i++) if(isprint(str[i])) printf("str[%d] is printable character:%d\n",i,str[i]); } |
|
执行
|
str[0] is printable character:a str[1] is printable character:5 str[2] is printable character: str[3] is printable character:@ str[4] is printable character:; |
|
|
|
|
|
isspace(测试字符是否为空格字符) |
|
相关函数
|
isgraph |
|
表头文件
|
#include<ctype.h> |
|
定义函数
|
int isspace(int c) |
|
函数说明
|
检查参数c是否为空格字符,也就是判断是否为空格('')、定位字符('\t')、CR('\r')、换行('\n')、垂直定位字符('\v')或翻页('\f')的情况。 |
|
返回值
|
若参数c为空格字符,则返回TRUE,否则返回NULL(0)。 |
|
附加说明
|
此为宏定义,非真正函数。 |
|
范例
|
/*将字符串str[]中内含的空格字符找出,并显示空格字符的ASCII码*/ #include <ctype.h> main() { char str="123c @# FD\tsP[e?\n"; 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]); } |
|
执行
|
str[4] is a white-space character:32 str[7] is a white-space character:32 str[10] is a white-space character:9 /* \t */ str[16] is a white-space character:10 /* \t */ |
|
|
|
|
|
ispunct(测试字符是否为标点符号或特殊符号) |
|
相关函数
|
isspace,isdigit,isalpha |
|
表头文件
|
#inlude<ctype.h> |
|
定义函数
|
int ispunct(int c) |
|
函数说明
|
检查参数c是否为标点符号或特殊符号。返回TRUE也就是代表参数c为非空格、非数字和非英文字母。 |
|
返回值
|
v若参数c为标点符号或特殊符号,则返回TRUE,否则返回NULL(0)。 |
|
附加说明
|
此为宏定义,非真正函数。 |
|
范例
|
/*列出字符串str中的标点符号或特殊符号*/ #include <ctype.h> main() { char str[]="123c@ #FDsP[e?"; int i; for(i=0;str[i]!=0;i++) if(ispunct(str[i])) printf("%c\n",str[i]); } |
|
执行
|
v @#[? |
|
|
|
|
|
isupper(测试字符是否为大写英文字母) |
|
相关函数
|
isalpha,islower |
|
表头文件
|
#include<ctype.h> |
|
定义函数
|
int isupper(int c) |
|
函数说明
|
检查参数c是否为大写英文字母。 |
|
返回值
|
若参数c为大写英文字母,则返回TRUE,否则返回NULL(0)。 |
|
附加说明
|
此为宏定义,非真正函数。 |
|
范例
|
/*找出字符串str中为大写英文字母的字符*/ #include <ctype.h> main() { char str[]="123c@#FDsP[e?"; int i; for(i=0;str[i]!=0;i++) if(isupper(str[i])) printf("%c is an uppercase character\n",str[i]); } |
|
执行
|
F is an uppercase character D is an uppercase character P is an uppercase character |
|
|
|
|
|
isxdigit(测试字符是否为16进制数字) |
|
相关函数
|
isalnum,isdigit |
|
表头文件
|
#include<ctype.h> |
|
定义函数
|
int isxdigit (int c) |
|
函数说明
|
检查参数c是否为16进制数字,只要c为下列其中一个情况则返回TRUE。16进制数字:0123456789ABCDEF。 |
|
返回值
|
若参数c为16进制数字,则返回TRUE,否则返回NULL(0)。 |
|
附加说明
|
此为宏定义,非真正函数。 |
|
范例
|
/*找出字符串str中为十六进制数字的字符*/ #include <ctype.h> main() { char str[]="123c@#FDsP[e?"; int i; for(i=0;str[i]!=0;i++) if(isxdigit(str[i])) printf("%c is a hexadecimal digits\n",str[i]); } |
|
执行
|
1 is a hexadecimal digits 2 is a hexadecimal digits 3 is a hexadecimal digits c is a hexadecimal digits F is a hexadecimal digits D is a hexadecimal digits e is a hexadecimal digits |
Linux实例程序代码如下:
#include <stdio.h>
#include <ctype.h>
void isalnum_fun(char *str);
void isalpha_fun(char *str);
void isdigit_fun(char *str);
void isgraphis_fun(char *str);
void islower_fun(char *str);
void isprint_fun(char *str);
void isspace_fun(char *str);
void ispunct_fun(char *str);
void isupper_fun(char *str);
void isxdigit_fun(char *str);
int main()
{
int Index;
char str[];
printf("1.isalnum_fun()\n");
printf("2.isalpha_fun()\n");
printf("3.isdigit_fun()\n");
printf("4.isgraphis_fun()\n");
printf("5.islower_fun()\n");
printf("6.isprint_fun()\n");
printf("7.isspace_fun()\n");
printf("8.ispunct_fun()\n");
printf("9.isupper_fun()\n");
printf("10.isxdigit_fun()\n");
printf("Please input the String:\n");
gets(str);
while()
{
printf("Please input the operator you want done (0-exit):\n");
scanf("%d",&Index);
if(Index==)
{
printf("Bye Bye!\n");
break;
}
else
{
switch(Index)
{
case :isalnum_fun(str);
break;
case :isalpha_fun(str);
break;
case :isdigit_fun(str);
break;
case :isgraphis_fun(str);
break;
case :islower_fun(str);
break;
case :isprint_fun(str);
break;
case :isspace_fun(str);
break;
case :ispunct_fun(str);
break;
case :isupper_fun(str);
break;
case :isxdigit_fun(str);
break;
default:
break;
}
}
}
return ;
}
void isalnum_fun(char *str)
{
int i;
for(i=;str[i]!=;i++);
{
if(isalnum(str[i]))
printf("%c is an alphanumberic character\n",str[i]);
}
}
void isalpha_fun(char *str)
{
int i;
for(i=;str[i]!=;i++)
{
if(isalpha(str[i]))
printf("%c is an alphanumberic character\n",str[i]);
}
}
void isdigit_fun(char *str)
{
int i;
for(i=;str[i]!=;i++)
{
if(isdigit(str[i]))
printf("%c is an digit character\n",str[i]);
}
}
void isgraphis_fun(char *str)
{
int i;
for(i=;str[i]!=;i++)
{
if(isgraph(str[i]))
printf("str[%d] is printable character:%c\n",i,str[i]);
}
}
void islower_fun(char *str)
{
int i;
for(i=;str[i]!=;i++)
{
if(islower(str[i]))
printf("%c is an low_case character\n",str[i]);
}
}
void isprint_fun(char *str)
{
int i;
for(i=;str[i]!=;i++)
{
if(isprint(str[i]))
printf("str[%d] is printable character:%c\n",i,str[i]);
}
}
void isspace_fun(char *str)
{
int i;
for(i=;str[i]!=;i++)
{
if(isspace(str[i]))
printf("str[%d] is a white_space character:%d\n",i,str[i]);
}
}
void ispunct_fun(char *str)
{
int i;
for(i=;str[i]!=;i++)
{
if(ispunct(str[i]))
printf("%c\n",str[i]);
}
}
void isupper_fun(char *str)
{
int i;
for(i=;str[i]!=;i++)
{
if(isupper(str[i]))
printf("%c is an upper_case character\n",str[i]);
}
}
void isxdigit_fun(char *str)
{
int i;
for(i=;str[i]!=;i++)
{
if(isxdigit(str[i]))
printf("%c is an hex digits\n",str[i]);
}
}
运行结果图:
Linux常用C函数---字符测试篇的更多相关文章
- Linux常用C函数---字符串转换篇
函数讲解部分参考http://net.pku.edu.cn/~yhf/linux_c/ atof(将字符串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul 表 ...
- Linux常用C函数-接口处理篇(网络通信函数)
接口处理篇accept,bind,connect,endprotoent,endservent,getsockopt,htonl,htons,inet_addr,inet_aton,inet_ntoa ...
- Linux常用C函数---内存控制篇
函数讲解部分参考http://net.pku.edu.cn/~yhf/linux_c/ calloc(配置内存空间) 相关函数 malloc,free,realloc,brk 表头文件 #includ ...
- linux常用C函数目录
字符测试篇 isalnum isalpha isascii iscntrl isdigit isgraphis islower isprint isspace ispunct isupper isxd ...
- Linux常用系统函数
Linux常用系统函数 一.进程控制 fork 创建一个新进程clone 按指定条件创建子进程execve 运行可执行文件exit 中止进程_exit 立即中止当前进程getdtablesize 进程 ...
- Linux常用命令及搭建测试环境
题外话:三大操作系统------Linux.Unix.Windows,Unix系统如常见的Mac OS,Linux的很多命令跟Unix是通用的,所以就有一些开发人猿喜欢用苹果的原因.Linux发行版特 ...
- Linux 常用命令:开发调试篇
前言 Linux常用命令中有一些命令可以在开发或调试过程中起到很好的帮助作用,有些可以帮助了解或优化我们的程序,有些可以帮我们定位疑难问题.本文将简单介绍一下这些命令. 示例程序 我们用一个小程序,来 ...
- Linux 常用命令:文本查看篇
前言 Linux常用命令中,除了cat还有很多其他用于文本查看的命令.本文将简单介绍一下这些文本查看的命令. 全文本显示--cat cat可能是常用的一个文本查看命令了,使用方法也很简单: cat f ...
- Linux常用命令速查-汇总篇
Linux常用命令速查-用户管理 Linux常用命令速查-文件管理 Linux常用命令速查-系统监控 Linux常用命令速查-网络管理 Linux常用命令速查-定时任务 Linux常用命令速查-Vim
随机推荐
- 【转】Compile、Make和Build的区别
原文网址:http://lavasoft.blog.51cto.com/62575/436216 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任 ...
- Java 的zip压缩和解压缩
Java 的zip压缩和解压缩 好久没有来这写东西了,今天中秋节,有个东西想拿出来分享,一来是工作中遇到的问题,一来是和csdn问候一下,下面就分享一个Java中的zip压缩技术,代码实现比较简单,代 ...
- android面试题之五
二十六.什么情况会导致Force Close ?如何避免?能否捕获导致其的异常? 抛出运行时异常时就会导致Force Close,比如空指针.数组越界.类型转换异常等等. 捕获:可以通过logcat查 ...
- bootstrap之Flick
Flick package io.appium.android.bootstrap.handler; import com.android.uiautomator.core.UiDevice; imp ...
- 获取系统的IP
如何获取Linux系统的ip.当时一直用这种方法获取IP的. ifconfig|grep 'Bcast'|awk -F ':' '{print $2}'|awk '{print $1}' 今天偶然发现 ...
- NET基础课--配置文件2
1. 使用<appSettings> 简单的配置信息,可以直接放入<appSettings>标记中.如: <?xml version="1.0& ...
- jquery优化引发的思考
无意间看到jquery优化的一个细节让我觉得不可思议记录一下.仅仅只是换个地方代码就能提高数倍的效率,带给我的不是个仅是个小技巧,而是一总编程思想,技术大牛往往是在细节上体现. 通过缓存最小化选择操作 ...
- log4.net 日志工具使用
1. 在应用程序的相同目录下建立: winform : 程序名.exe.config .(log4net程序,就log4net.exe.config) web: ...
- UINavigationController 和 UITabBarController
UINavigationController当设置根控制器的时候,意思就是把根控制器压入栈内,当我们push的时候,我们把下一个控制器压入栈内,当我们pop的时候把上面的控制器的内存释放 UITa ...
- 【iOS】objective-c 文档生成工具 appledoc
最近做ios framework的一些测试,提供给其他开发者使用的framework,API文档变得更加重要,以前没有接触过,这次尝试使用了一把appledoc来生成一下文档,感觉还不错. 首先,是从 ...