首页 > 代码库 > ctype.h函数源码

ctype.h函数源码

iscsym()函数源码

 

int my_iscsym(int c)

{

  return (isalnum(c) || ( c == ‘_‘ ));

}//判断字符c是否为英文字母、数字和下划线

int main()

{

   char ch = ‘a‘;

 

   if(my_iscsym(ch))

      printf("%c is English, UnderLine Character or Number! /n",ch);

   else

      printf("%c is not a English,  UnderLine Character and Number!/n",ch);

    ch = ‘_‘;

 

   if(my_iscsym(ch))

      printf("%c is English, UnderLine Character or Number! /n",ch);

   else

      printf("%c is not a English,  UnderLine Character and Number!/n",ch);

 

   ch = ‘1‘;

 

   if(my_iscsym(ch))

      printf("%c is English, UnderLine Character or Number! /n",ch);

   else

      printf("%c is not a English,  UnderLine Character and Number!/n",ch);

  

   ch = ‘?‘;

 

   if(my_iscsym(ch))

      printf("%c is English, UnderLine Character or Number! /n",ch);

   else

      printf("%c is not a English,  UnderLine Character and Number!/n",ch);

  

   system("pause");

   return 0;

}

 

iscsymf()函数源码

int my_iscsymf(int c)

{

  return (isalpha(c) || ( c == ‘_‘ ));

}//判断字符c是否为英文字母和下划线

int main()

{

   char ch = ‘a‘;

 

   if(my_iscsymf(ch))

      printf("%c is English or UnderLine Character! /n",ch);

   else

      printf("%c is not a English and UnderLine Character!/n",ch);

 

   ch = ‘_‘;

 

   if(my_iscsymf(ch))

      printf("%c is English or UnderLine Character! /n",ch);

   else

      printf("%c is not a English and UnderLine Character!/n",ch);

 

   ch = ‘1‘;

 

   if(my_iscsymf(ch))

      printf("%c is English or UnderLine Character! /n",ch);

   else

      printf("%c is not a English and UnderLine Character!/n",ch);

 

   system("pause");

   return 0;

}

 

toascii()函数源码

int my_toascii( int c)

{

    return c & 0x7f;

}//将字符c转换为ascii码。toascii函数将字符c的高位清零,仅保留低七位。返回转换后的数值。

int main()

{

   unsigned char str[] = "杨晓曼";

   unsigned char *p = str;

  

   while(*p)

   {

      *p =my_toascii(*p);

      p++;

   }

   puts(str);

 

   system("pause");

   return 0;

}

 

isascii()函数源码

int my_isascii( int ch )

{

   return (unsigned int)ch < 128u;

}//判断字符c是否为ascii码。ascii码指0x00-0x7F之间的字符。

int main()

{

   int ch = ‘a‘;

 

   if(my_isascii(ch))

      printf("%c is ASCII Character!/n",ch);

   else

      printf("%c is not a ASCII Character!/n",ch);

 

   ch = 0x80;

 

   if(my_isascii(ch))

      printf("0x%x is ASCII Character!/n",ch);

   else

      printf("0x%x is not a ASCII Character!/n",ch);

 

   system("pause");

   return 0;

}

 

tolower()函数源码

int my_tolower( int ch)

{

   if ( (unsigned int)(ch - ‘A‘) < 26u )

      ch += ‘a‘ - ‘A‘;

   return ch;

}//大写字母转换为小写字母。

int main()

{

   char str[] = "AMMANA_BaBI";

   char *p = str;

 

   while(*p)

   {

      *p =my_tolower(*p);

      p++;

   }

   puts(str);

 

   system("pause");

   return 0;

}

 

toupper()函数源码

int my_toupper( int ch)

{

   if ( (unsigned int)(ch - ‘a‘) < 26u )

      ch += ‘A‘ - ‘a‘;

   return ch;

}//小写字母转换为大写字母。

int main()

{

   char str[] = "ammana_Babi";

   char *p = str;

 

   while(*p)

   {

      *p =my_toupper(*p);

      p++;

   }

   puts(str);

 

   system("pause");

   return 0;

}

 

iscntrl()函数源码

int my_iscntrl( int ch )

{

    return (unsigned int)ch < 32u  ||  ch == 127;

}//判断字符c是否为控制字符。当c在0x00-0x1F之间或等于0x7F(DEL)时,返回非零值,否则返回零。

int main()

{

   char ch = ‘_‘;

 

   if(my_iscntrl(ch))

      printf("%c is a Ctrol Character/n",ch);

   else

      printf("%c is not a Ctrol Character!/n",ch);

 

   ch = 0x10;

 

   if(my_iscntrl(ch))

      printf("0x%x is a Ctrol Character/n",ch);

   else

      printf("0x%x is not a Ctrol Character!/n",ch);

 

   ch = ‘ ‘;

 

   if(my_iscntrl(ch))

      printf("%c is a Ctrol Character/n",ch);

   else

      printf("%c is not a Ctrol Character!/n",ch);

 

   system("pause");

   return 0;

}

 

isgraph()函数源码

int my_isgraph( int ch )

{

  return (unsigned int)(ch - ‘!‘) < 127u - ‘!‘;

}//判断字符c是否为除空格外的可打印字符。可打印字符(0x21-0x7e)。

int main()

{

   char ch = ‘_‘;

 

   if(my_isgraph(ch))

      printf("%c is a Graph Character/n",ch);

   else

      printf("%c is not a Graph Character!/n",ch);

 

   ch = ‘1‘;

 

   if(my_isgraph(ch))

      printf("%c is a Graph Character/n",ch);

   else

      printf("%c is not a Graph Character!/n",ch);

 

   ch = ‘ ‘;

 

   if(my_isgraph(ch))

      printf("%c is a Graph Character/n",ch);

   else

      printf("%c is not a Graph Character!/n",ch);

 

   system("pause");

   return 0;

}

 

isprint()函数源码

int my_isprint( int ch )

{

    return (unsigned int)(ch - ‘ ‘) < 127u - ‘ ‘;

}//判断字符c是否为可打印字符(含空格)。当c为可打印字符(0x20-0x7e)时,返回非零值,否则返回零。

int main()

{

   char ch = ‘_‘;

 

   if(my_isprint(ch))

      printf("%c is a Sprint Character/n",ch);

   else

      printf("%c is not a Sprint Character!/n",ch);

 

   ch = ‘1‘;

 

   if(my_isprint(ch))

      printf("%c is a Sprint Character/n",ch);

   else

      printf("%c is not a Sprint Character!/n",ch);

 

   ch = 0x10;

 

   if(my_isprint(ch))

      printf("0x%x is a Sprint Character/n",ch);

   else

      printf("0x%x is not a Sprint Character!/n",ch);

 

   system("pause");

   return 0;

}

 

isalnum()函数源码

int my_isalnum ( int ch )

{

   return (unsigned int)((ch | 0x20) - ‘a‘) < 26u  ||

         (unsigned int)( ch - ‘0‘) < 10u;

}//测试参数ch是否是字母(A-Z,大小写均可)或数字(0-9)

int main()

{

   char ch = ‘c‘;

 

   if(my_isalnum(ch))

      printf("%c is English Character or Number!/n",ch);

   else

      printf("%c is not a English Character and Number!/n",ch);

 

   ch = ‘1‘;

 

   if(my_isalnum(ch))

      printf("%c is English Character or Number!/n",ch);

   else

      printf("%c is not a English Character and Number!/n",ch);

 

   ch = ‘!‘;

 

   if(my_isalnum(ch))

      printf("%c is English Character or Number!/n",ch);

   else

      printf("%c is not a English Character and Number!/n",ch);

 

   system("pause");

   return 0;

}

 

ispunct()函数源码

int my_ispunct( int ch )

{

 return isprint(ch)  &&  !isalnum (ch)  &&  !isspace (ch);

}//判断字符c是否为标点符号。标点符号指那些既不是字母数字,也不是空格的可打印字符。

 

int main()

{

   char ch = ‘,‘;

 

   if(my_ispunct(ch))

      printf("%c is punctuation Character!/n",ch);

   else

      printf("%c is not a punctuation Character!/n",ch);

 

   ch = ‘!‘;

 

   if(my_ispunct(ch))

      printf("%c is punctuation Character!/n",ch);

   else

      printf("%c is not a punctuation Character!/n",ch);

 

   ch = ‘1‘;

 

   if(my_ispunct(ch))

      printf("%c is punctuation Character!/n",ch);

   else

      printf("%c is not a punctuation Character!/n",ch);

 

   system("pause");

   return 0;

}

 

isspace()函数源码

int my_isspace( int ch )

{

    return (unsigned int)(ch - 9) < 5u  ||  ch == ‘ ‘;

}//判断字符c是否为空白符。空白符指空格、水平制表、垂直制表、换页、回车和换行符。

 

int main()

{

   char ch = ‘ ‘;

 

   if(my_isspace(ch))

      printf("%c is Space Character!/n",ch);

   else

      printf("%c is not a Space Character!/n",ch);

 

   ch = ‘/n‘;

 

   if(my_isspace(ch))

      printf("%c is Space Character!/n",ch);

   else

      printf("%c is not a Space Character!/n",ch);

 

   ch = ‘1‘;

 

   if(my_isspace(ch))

      printf("%c is Space Character!/n",ch);

   else

      printf("%c is not a Space Character!/n",ch);

 

   system("pause");

   return 0;

}

 

isxdigit()函数源码

int my_isxdigit( int ch )

{

    return (unsigned int)( ch         - ‘0‘) < 10u  ||

           (unsigned int)((ch | 0x20) - ‘a‘) <  6u;

}//判断字符c是否为十六进制数字。

//当c为A-F,a-f或0-9之间的十六进制数字时,返回非零值,否则返回零。

 

int main()

{

   int ch = ‘g‘;

 

   if(my_isxdigit(ch))

      printf("%c is Hex Number!/n",ch);

   else

      printf("%c is not a Hex Number!/n",ch);

 

   ch = ‘1‘;

 

   if(my_isxdigit(ch))

      printf("%c is Hex Number!/n",ch);

   else

      printf("%c is not a Hex Number!/n",ch);

 

   system("pause");

   return 0;

}

 

isdigit()函数源码

int my_isdigit( int ch )

{

    return (unsigned int)(ch - ‘0‘) < 10u;

}//判断字符c是否为数字

int main()

{

   int ch = ‘A‘;

 

   if(my_isdigit(ch))

      printf("%c is Digital Number!/n",ch);

   else

      printf("%c is not a Digital Number!/n",ch);

 

   ch = ‘1‘;

 

   if(my_isdigit(ch))

      printf("%c is Digital Number!/n",ch);

   else

      printf("%c is not a Digital Number!/n",ch);

 

   system("pause");

   return 0;

}

 

islower()函数源码

int my_islower ( int ch )

{

    return (unsigned int) (ch - ‘a‘) < 26u;

}//判断字符c是否为小写英文字母

int main()

{

   char ch = ‘c‘;

 

   if(my_islower(ch))

      printf("%c is Lower English Character!/n",ch);

   else

      printf("%c is not a Lower English Character!/n",ch);

 

   ch = ‘A‘;

 

   if(my_islower(ch))

      printf("%c is Lower English Character!/n",ch);

   else

      printf("%c is not a Lower English Character!/n",ch);

 

   system("pause");

   return 0;

}

 

isupper()函数源码

int my_isupper( int ch )

{

    return (unsigned int)(ch - ‘A‘) < 26u;

}//判断字符c是否为大写英文字母

 

int main()

{

   char ch = ‘c‘;

 

   if(my_isupper(ch))

      printf("%c is Upper English Character!/n",ch);

   else

      printf("%c is not a Upper English Character!/n",ch);

 

   ch = ‘A‘;

 

   if(my_isupper(ch))

      printf("%c is Upper English Character!/n",ch);

   else

      printf("%c is not a Upper English Character!/n",ch);

 

   system("pause");

   return 0;

}

 

isalpha()函数源码

int my_isalpha(int ch )

{

   return (unsigned int)((ch | 0x20) - ‘a‘) < 26u;

}//判断字符c是否为英文字母

int main()

{

   char ch = ‘c‘;

 

   if(my_isalpha(ch))

      printf("%c is English Character!/n",ch);

   else

      printf("%c is not a English Character!/n",ch);

 

   ch = ‘A‘;

 

   if(my_isalpha(ch))

      printf("%c is English Character!/n",ch);

   else

      printf("%c is not a English Character!/n",ch);

 

   ch = ‘1‘;

 

   if(my_isalpha(ch))

      printf("%c is English Character!/n",ch);

   else

      printf("%c is not a English Character!/n",ch);

 

   system("pause");

   return 0;

}

ctype.h函数源码