qey

Atitude is Everything.-- 关注C/C++,关注Linux(Unix) ,关注网络。 Better Late Than Never.
随笔 - 4, 文章 - 13, 评论 - 7, 引用 - 0
数据加载中……

ctype.h 中常用函数:字符类型判断及简单转换

C语言中<ctype.h> ,在C++中为<cctype>

本文下面列出这个头文件的常用函数。目的是了解字符判断函数和学习简洁代码编写。

注意:C++中的<cctype>和<cwtype>头文件继承于C。在许多实现方式中,函数在s记命名空间内部和外部都定义了,
以允许旧式C程序编译和链接。此时,函数名无论是否带std限定符都能工作,但因为我们编写的是C++程序,所以应
限定名称。

isalnum()    如果参数为字母数字,则返回ture
isalpha()     如果参数为字母,则返回ture
iscntrl()       如果参数为控制字符,则返回ture
isdigit()       如果参数为数字(0-9),则返回ture
isgraph()     如果参数为空格之外的打印字符,则返回ture
islower()     如果参数是小写字母,则返回ture
isprint()       如果参数为打印字符,包括空格,则返回ture
ispunct()      如果参数为标点符号,则返回ture
isspace()      如果参数为标准空白字符(空格,进纸,换行符,回车,水平制表符,垂直制表符),则返回ture
siupper()      如果参数为大写字母,则返回ture
isxdigit()      如果参数为十六进制数,则返回ture
tolower()     如果参数为大写字母,即转化为小写字母,否则返回原值
toupper()     如果参数为小写字母,即转化为大写字母,否则返回原值

// *******************************************************
// 判断字符c是否为英文字母:::0x41 0x61,如果是大写字母,
// 将通过(ch | 0x20)转为小写字母值来判断
int isalpha(int ch ) 
{
    
return (unsigned int)((ch | 0x20- 'a'< 26u;
}


// *******************************************************
// 判断字符c是否为大写英文字母
int isupper( int ch )
{
    
return (unsigned int)(ch - 'A'< 26u;
}


// *******************************************************
// 判断字符c是否为小写英文字母
int islower ( int ch ) 
{
    
return (unsigned int) (ch - 'a'< 26u;
}


// *******************************************************
// 判断字符c是否为数字
int isdigit( int ch )
{
    
return (unsigned int)(ch - '0'< 10u;
}


// *******************************************************
// 判断字符c是否为十六进制数字。
// 当c为A-F,a-f或0-9之间的十六进制数字时,返回非零值。
int isxdigit( int ch )
{
    
return (unsigned int)( ch -'0'< 10u || 
        (unsigned 
int)((ch | 0x20- 'a'< 6u;
}


// *******************************************************
// 判断字符c是否为空白符。空白符指空格、水平制表、垂直制表、换页、回车和换行符。
int isspace( int ch )
{
    
return (unsigned int)(ch - 9< 5u  ||  ch == ' ';
}


// *******************************************************
// 判断字符c是否为标点符号。标点符号指那些既不是字母数字,也不是空格的可打印字符。
int ispunct( int ch ) 
{
    
return isprint(ch)  &&  !isalnum (ch)  &&  !isspace (ch);
}


// *******************************************************
// 测试参数ch是否是字母(A-Z,大小写均可)或数字(0-9)
int isalnum ( int ch ) 
{
    
return (unsigned int)((ch | 0x20- 'a'< 26u  ||
        (unsigned 
int)( ch - '0'< 10u;
}


// *******************************************************
// 判断字符c是否为可打印字符(含空格)。当c为可打印字符(0x20-0x7e)时,
// 返回非零值,否则返回零。
int isprint( int ch )
{
    
return (unsigned int)(ch - ' '< 127u - ' ';
}


// *******************************************************
// 判断字符c是否为除空格外的可打印字符。可打印字符(0x21-0x7e)
int isgraph( int ch ) 
{
    
return (unsigned int)(ch - '!'< 127u - '!';
}


// *******************************************************
// 判断字符c是否为控制字符。当c在0x00-0x1F之间或等于0x7F(DEL)时,
// 返回非零值,否则返回零。
int iscntrl( int ch ) 
{
    
return (unsigned int)ch < 32u  ||  ch == 127;
}


// *******************************************************
// 小写字母转换为大写字母。
int toupper( int ch) 
{
    
if ( (unsigned int)(ch - 'a'< 26u )
        ch 
+= 'A' - 'a';
    
return ch;
}

// 返回一个按位与不就行了吗??简练为好,不做检测
// return (unsigned int)(ch | 0x20);
// tolower 类似表示为 return (unsigned int)(ch & 0x5F);

// *******************************************************
// 大写字母转换为小写字母。
int tolower( int ch) 
{
    
if ( (unsigned int)(ch - 'A'< 26u )
        ch 
+= 'a' - 'A';
    
return ch;
}


// *******************************************************
// 判断字符c是否为ascii码。ascii码指0x00-0x7F之间的字符。
int isascii( int ch ) 
{
    
return (unsigned int)ch < 128u;
}


//*******************************************************
//将字符c转换为ascii码。toascii函数将字符c的高位清零,仅保留低七位。返回转换后的数值。
int toascii( int c)
{
    
return c & 0x7f;
}


//*******************************************************
//判断字符c是否为英文字母和下划线
int iscsymf(int c)
{
    
return (isalpha(c) || ( c == '_' ));
}


// *******************************************************
// 判断字符c是否为英文字母、数字和下划线
int iscsym(int c)
{
    
return (isalnum(c) || ( c == '_' ));
}

posted on 2006-06-15 19:17 无声无色 阅读(1008) 评论(0)  编辑 收藏 引用 所属分类: 语言基础


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理