ctype.h里的函数(c语言)

来源:互联网 发布:免费淘宝海报在线制作 编辑:程序博客网 时间:2024/04/28 10:11


/*
 * 主要测试该头文件中的如下函数:
 * isalnum , isdigit , isalpha , isascii , iscntrl , ispunct
 * isgraphics , isprint , islower , isupper , isxdigit
 * 需要注意的是:这些均为宏定义,非真正函数。
 * int isalnum (int c) 测试字符是否为英文或数字,在标准c中相当于使用“isalpha(c) || isdigit(c)”
 * int isalpha (int c) 检查参数c是否为英文字母 ,在标准c中相当于使用“isupper(c)||islower(c)”
 * int isascii(int c)  检查参数c是否为ASCII码字符,也就是判断c的范围是否在0到127之间。
 * int iscntrl(int c)  检查参数c是否为ASCII控制码,也就是判断c的范围是否在0到30之间。
 * int isdigit(int c)  检查参数c是否为阿拉伯数字0到9。
 * int isgraph (int c) 检查参数c是否为可打印字符,若c所对映的ASCII码可打印,且非空格字符则返回TRUE。
 * int islower(int c)  检查参数c是否为小写英文字母。
 * int isprint(int c)  检查参数c是否为可打印字符,若c所对映的ASCII码可打印,其中包含空格字符,则返回TRUE。
 * int isspace(int c)  检查参数c是否为空格字符,也就是判断是否为空格('')、定位字符('\t')、CR('\r')、换行('\n')、垂直定位字符('\v')或翻页('\f')的情况。
 * int ispunct(int c)  检查参数c是否为标点符号或特殊符号。返回TRUE也就是代表参数c为非空格、非数字和非英文字母。
 * int isupper(int c)  检查参数c是否为大写英文字母。
 * int isxdigit (int c)检查参数c是否为16进制数字,只要c为下列其中一个情况则返回TRUE。16进制数字:0123456789ABCDEF。
 
*/

ctype.h

  ctype.h里的函数

  1 字符测试函数

  1> 函数原型均为int isxxxx(int)

  2> 参数为int, 任何实参均被提升成整型

  3> 只能正确处理处于[0, 127]之间的值

  2 字符映射函数

  1> 函数原型为int toxxxx(int)

  2> 对参数进行检测, 若符合范围则转换, 否则不变

  int tolower(int); 'A'~'Z' ==>'a'~'z'

  int toupper(int); 'a'~'z' ==>'A'~'Z'

  @函数名称: isalpha

  函数原型: int isalpha(int ch);

  函数功能: 检查ch是否是字母.

  函数返回: 是字母返回非0 ,否则返回 0.

  参数说明:

  所属文件 <ctype.h>

  #include <stdio.h>

  #include <ctype.h>

  int main()

  {

  char ch1='*';

  char ch2='a';

  if(isalnum(ch1)!=0)

  printf("%c is the ASCIInumber or alphebet\n",ch1);

  else

  printf("%c is not the ASCIInumber nor alphebet\n",ch1);

  if(isalnum(ch2)!=0)

  printf("%c is the ASCIInumber or alphebet\n",ch2);

  else

  printf("%c is not the ASCIInumber nor alphebet\n",ch2);

  return 0;

  }

  @函数名称: iscntrl

  函数原型: int iscntrl(int ch);

  函数功能: 检查ch是否控制字符(其ASCII码在0和0x1F之间,数值为 0-31).

  函数返回: 是返回 1,否则返回 0.

  参数说明:

  所属文件: <ctype.h>

  #include <stdio.h>

  #include <ctype.h>

  char chars[]={'A',0x09,'Z'};

  #define SIZEsizeof(chars)/sizeof(char)

  int main()

  {

  int i;

  for(i=0;i<SIZE;i++){

  printf("Char %c is %saControl character\n",

  chars[i],(iscntrl(chars[i]))?"":"not");

  }

  return 0;

  }

  @函数名称: isdigit

  函数原型: int isdigit(int ch);

  函数功能: 检查ch是否是数字(0-9)

  函数返回: 是返回1,否则返回0

  参数说明:

  所属文件: <ctype.h>

  #include <stdio.h>

  #include <ctype.h>

  int main()

  {

  char ch1='1';

  char ch2='a';

  if(isdigit(ch1)!=0)

  printf("%c is the ASCIInumber\n",ch1);

  else

  printf("%c is not the ASCIInumber\n",ch1);

  if(isdigit(ch2)!=0)

  printf("%c is the ASCIInumber\n",ch2);

  else

  printf("%c is not the ASCIInumber\n",ch2);

  return 0;

  }

  @函数名称: isgraph

  函数原型: int isgraph(int ch);

  函数功能: 检查ch是否可显示字符(其ASCII码在ox21到ox7E之间),不包括空格

  函数返回: 是返回1,否则返回0

  参数说明:

  所属文件: <ctype.h>

  #include <stdio.h>

  #include <ctype.h>

  int main()

  {

  char ch1=' ';

  char ch2='a';

  if(isgraph(ch1)!=0)

  printf("%c is the ASCIIprintable character\n",ch1);

  else

  printf("%c is not the ASCIIprintable character\n",ch1);

  if(isgraph(ch2)!=0)

  printf("%c is the ASCIIprintable character\n",ch2);

  else

  printf("%c is not the ASCIIprintable character\n",ch2);

  return 0;

  }

  @函数名称: islower

  函数原型: int islower(int ch);

  函数功能: 检查ch是否小写字母(a-z)

  函数返回: 是返回1,否则返回0

  参数说明:

  所属文件: <ctype.h>

  #include <stdio.h>

  #include <ctype.h>

  char chars[]={'A','a','z','Z'};

  #define SIZEsizeof(chars)/sizeof(char)

  int main()

  {

  int i;

  for(i=0;i<SIZE;i++){

  printf("Char %c is %salowercasecharacter\n",chars[i],(islower(chars[i]))?"":"not");

  }

  return 0;

  }

  @函数名称: tolower

  函数原型: int tolower(int ch);

  函数功能: 将ch字符转换为小写字母

  函数返回: 返回ch所代表的字符的小写字母

  参数说明:

  所属文件: <ctype.h>

  #include <stdio.h>

#include <stdlib.h>

  #include <ctype.h>

  int main()

  {

  char x='a', y='b',z='A',w='*';

  printf("Character %c tolower is %c\n",x,tolower(x));

  printf("Character %c tolower is %c\n",y,tolower(y));

  printf("Character %c tolower is %c\n",z,tolower(z));

  printf("Character %c tolower is %c\n",w,tolower(w));

  return 0;

  }

  @函数名称: toupper

  函数原型: int toupper(int ch);

  函数功能: 将ch字符转换成大写字母

  函数返回: 与ch相应的大写字母

  参数说明:

  所属文件: <ctype.h>

  #include <stdio.h>

  #include <stdlib.h>

  #include <ctype.h>

  int main()

  {

  char x='a', y='b',z='A',w='*';

  printf("Character %c toupper is %c\n",x,toupper(x));

  printf("Character %c toupper is %c\n",y,toupper(y));

  printf("Character %c toupper is %c\n",z,toupper(z));

  printf("Character %c toupper is %c\n",w,toupper(w));

  return 0;

  }

  @函数名称: isalnum

  函数原型: int isalnum(int ch);

  函数功能: 检查ch是否是字母或数字

  函数返回: 是字母或数字返回1,否则返回0

  参数说明:

  所属文件: <ctype.h>

  #include <stdio.h>

  #include <ctype.h>

  int main()

  {

  char ch1 ='*';

  char ch2 ='a';

  if(isalnum(ch1)!=0)

  printf("%c is the ASCIInumber or alphebet\n",ch1);

  else

  printf("%c is not the ASCIInumber nor alphebet\n",ch1);

  if(isalnum(ch2)!=0)

  printf("%c is the ASCIInumber or alphebet\n",ch2);

  else

  printf("%c is not the ASCIInumber nor alphebet\n",ch2);

  return 0;

  }

  @函数名称: isprint

  函数原型: int isprint(int ch);

  函数功能: 检查ch是否是可打印字符(包括空格),其ASCII码在ox20到ox7E之间

  函数返回: 是返回1,否则返回0

  参数说明:

  所属文件: <ctype.h>

  #include <stdio.h>

  #include <ctype.h>

  int main()

  {

  char ch1='\n';

  char ch2='a';

  if(isprint(ch1)!=0)

  printf("%c is the ASCIIprintable charcater\n",ch1);

  else

  printf("%c is not the ASCII printablecharcater\n",ch1);

  if(isprint(ch2)!=0)

  printf("%c is the ASCIIprintable charcater\n",ch2);

  else

  printf("%c is not the ASCIIprintable charcater\n",ch2);

  return 0;

  }

  @函数名称: ispunct

  函数原型: int ispunct(int ch);

  函数功能: 检查ch是否是标点字符(不包括空格),即除字母,数字和空格以外的所有可打印字符

  函数返回: 是返回1,否则返回0

  参数说明:

  所属文件: <ctype.h>

  #include <stdio.h>

  #include<ctype.h>

  int main()

  {

  char ch1=',';

  char ch2='a';

  if(ispunct(ch1)!=0)

  printf("%c is the ASCIIpunct\n",ch1);

  else

  printf("%c is not the ASCIIpunct\n",ch1);

  if(ispunct(ch2)!=0)

  printf("%c is the ASCIIpunct\n",ch2);

  else

  printf("%c is not the ASCIIpunct\n",ch2);

  return 0;

  }

  @函数名称: isspace

  函数原型: int isspace(int ch);

  函数功能: 检查ch是否是空格符和跳格符(控制字符)或换行符

  函数返回: 是返回1,否则返回0

  参数说明:

  所属文件: <ctype.h>

  #include <stdio.h>

  #include <ctype.h>

  int main()

  {

  char ch1=' ';

  char ch2='a';

  if(isspace(ch1)!=0)

  printf("%c is the spacecharcater\n",ch1);

  else

  printf("%c is not the spacecharcater\n",ch1);

  if(isspace(ch2)!=0)

  printf("%c is the spacecharcater\n",ch2);

  else

  printf("%c is not the spacecharcater\n",ch2);

  return 0;

  }

  @函数名称: isupper

  函数原型: int isupper(int ch);

  函数功能: 检查ch是否是大写字母(A-Z)

  函数返回: 是返回1,否则返回0

  参数说明:

  所属文件: <ctype.h>

  #include <stdio.h>

  #include <ctype.h>

  char chars[]={'A','a','z','Z'};

  #define SIZEsizeof(chars)/sizeof(char)

  int main()

  {

  int i;

  for(i=0;i<SIZE;i++){

  printf("Char %c is %sanuppercase character\n",

  chars[i],(isupper(chars[i]))?"":"not");

  }

  return 0;

  }

  @函数名称: isxdigit

  函数原型: int isxdigit(int ch);

  函数功能: 检查ch是否是一个16进制数学字符(即0-9,或A-F,或a-f)

  函数返回: 是返回 1,否则返回0

  参数说明:

  所属文件: <ctype.h>

  #include <stdio.h>

  #include <ctype.h>

  int main()

  {

  char ch1='1';

  char ch2='a';

  if(isxdigit(ch1)!=0)

  printf("%c is the ASCIIhexadecimal number\n",ch1);

  else

  printf("%c is not the ASCIIhexadecimal number\n",ch1);

  if(isxdigit(ch2)!=0)

  printf("%c is the ASCIIhexadecimal number\n",ch2);

  else

  printf("%c is not the ASCIIhexadecimal number\n",ch2);

  return 0;

  }

  @函数名称: isascii

  函数原型: int isascii(int ch)

  函数功能: 测试参数是否是ASCII码0-127

  函数返回: 非零-是,0-不是

  参数说明: ch-被测参数

  所属文件: <ctype.h>

  #include <stdio.h>

  #include <ctype.h>

  char chars[]={'A',0x80,'Z'};

  #define SIZEsizeof(chars)/sizeof(char)

  int main()

  {

  int i;

  for(i=0;i<SIZE;i++)

  {

  printf("Char %c is %sanASCII character\n",

  chars[i],(isascii(chars[i]))?"":"not");

  }

  return 0;

  }










0 0