[转]Linux常用C函数-字符测试篇

来源:互联网 发布:学不会javascript 编辑:程序博客网 时间:2024/05/21 10:56

转自:http://blog.chinaunix.net/uid-20540874-id-1649504.html

字符测试篇isalnumisalphaisasciiiscntrlisdigitisgraphisislowerisprintisspaceispunctisupperisxdigitisalnum(测试字符是否为英文或数字)相关函数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 character2 is an apphabetic character3 is an apphabetic characterc is an apphabetic characterF is an apphabetic characterD is an apphabetic characters is an apphabetic characterP is an apphabetic charactere 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 characterF is an apphabetic characterD is an apphabetic characters is an apphabetic characterP is an apphabetic charactere 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);elseprintf("%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 character129 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 character2 is an digit character3 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:astr[1] is printable character:5str[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 characters is a lower-case charactere 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:astr[1] is printable character:5str[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:32str[7] is a white-space character:32str[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 characterD is an uppercase characterP 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 digits2 is a hexadecimal digits3 is a hexadecimal digitsc is a hexadecimal digitsF is a hexadecimal digitsD is a hexadecimal digitse is a hexadecimal digits


0 0