判断字符是否为英文字母

一个字节能表示0~255之间共256个数字,根据ASCII码英文字母A-Z和a-z按顺序排列,其中

  1. 'A' = 65  = 0b01000001 = 0x41  
  2. 'B' = 66  = 0b01000010 = 0x42  
  3. ...  
  4. 'Z' = 90  = 0b01011010 = 0x5a  
  5. 'a' = 97  = 0b01100001 = 0x61  
  6. 'b' = 98  = 0b01100010 = 0x62  
  7. ...  
  8. 'z' = 122 = 0b01111010 = 0x7a  
 

传统的判断方法是直接判断范围:

  1. #define judgeletter_classic(ch) (((ch)>='A'&&ch<='Z')||((ch)>='a'&&(ch)<='z'))  

但是仔细观察二进制部分会发现以下特点:

(1)所有字母最高两位一定是01

(2)从高位数第三位为0时为大写字母,1时为小写字母

(3)低5位从00001到11010共26种情况分别代表A-Z和a-z

所以得到以下通过分析位来判断的方法:

  1. #define judgeletter_bit(ch) (((ch)>>6)==1)&&((((ch)-1)&31)<26)  
 

还有一种方法叫查表法,首先构建一个表,把是字母的都标记为1,其他标记为0,这样就可以通过直接访问表中对应位置的数据得到判断:

  1. #define judgeletter_table(ch) (*(table + (ch)))  
 

建立表:

  1. unsigned char table[256];  
  2. memset(table, 0, 256);  
  3. for (i = 'A'; i <= 'Z'; i++)  
  4. {  
  5.   *(table + i) = 1;  
  6.   *(table + i + 'a' - 'A') = 1;  
  7. }  
 

最后C标准库内也自带了isalpha宏,可以判断是否为字母,在ctype.h里有声明:

  1. # define isalpha(c) __isctype((c), _ISalpha)  
 

现在我们来测试一下三种方法的速度,我们分别用三种方法循环判断0-255之间所有数字是否为ASCII码的英文字母,每种方法10000000次,然后输出所用时间,程序如下:

  1. #include <stdio.h>  
  2. #include <memory.h>  
  3. #include <ctype.h>  
  4. #include <sys/time.h>  
  5. #define TEST_TIMES 10000000  
  6. #define DEFINE_TIME /  
  7.   struct timeval time1, time2;/  
  8. #define START_RECORDING /  
  9.   gettimeofday(&time1, NULL);/  
  10. #define STOP_RECORDING /  
  11.   gettimeofday(&time2, NULL);/  
  12. #define PRINT_TIME /  
  13.   printf("%lu:%lu/n", time2.tv_sec - time1.tv_sec, time2.tv_usec - time1.tv_usec);    
  14. #define judgeletter_bit(ch) (((ch)>>6)==1)&&((((ch)-1)&31)<26)  
  15. #define judgeletter_classic(ch) (((ch)>='A'&&ch<='Z')||((ch)>='a'&&(ch)<='z'))  
  16. #define judgeletter_table(ch) (*(table + (ch)))  
  17. int main(int argc, const char *argv[])  
  18. {  
  19.   unsigned int ch;  
  20.   int i;  
  21.   int result;  
  22.   unsigned char table[256];  
  23.   memset(table, 0, 256);  
  24.   for (i = 'A'; i <= 'Z'; i++)  
  25.   {  
  26.     *(table + i) = 1;  
  27.     *(table + i + 'a' - 'A') = 1;  
  28.   }  
  29.   DEFINE_TIME;  
  30.   //Classic  
  31.   printf("classic:");  
  32.   START_RECORDING;  
  33.   for (i = 0; i < TEST_TIMES; i++) {  
  34.     for (ch = 0; ch < 256; ch++)  
  35.     {  
  36.       result = judgeletter_classic(ch);  
  37.     }  
  38.   }  
  39.   STOP_RECORDING;  
  40.   PRINT_TIME;  
  41.   //Bit  
  42.   printf("bit:");  
  43.   START_RECORDING;  
  44.   for (i = 0; i < TEST_TIMES; i++) {  
  45.     for (ch = 0; ch < 256; ch++)  
  46.     {  
  47.       result = judgeletter_bit(ch);  
  48.     }  
  49.   }  
  50.   STOP_RECORDING;  
  51.   PRINT_TIME;  
  52.   //Table  
  53.   printf("table:");  
  54.   START_RECORDING;  
  55.   for (i = 0; i < TEST_TIMES; i++) {  
  56.     for (ch = 0; ch < 256; ch++)  
  57.     {  
  58.       result = judgeletter_table(ch);  
  59.     }  
  60.   }  
  61.   STOP_RECORDING;  
  62.   PRINT_TIME;  
  63.   //ctype  
  64.   printf("isalpha:");  
  65.   START_RECORDING;  
  66.   for (i = 0; i < TEST_TIMES; i++) {  
  67.     for (ch = 0; ch < 256; ch++)  
  68.     {  
  69.       result = isalpha(ch);  
  70.     }  
  71.   }  
  72.   STOP_RECORDING;  
  73.   PRINT_TIME;  
  74.   return 0;  
  75. }  
 

 

我的机器使用gcc 4.4.5,无优化选项编译,运行得到的结果为:

  1. classic:15:701542  
  2. bit:11:172520  
  3. table:16:4294363296  
  4. isalpha:43:442001  

由此可见:采用bit 运算的效率最高,最节省时间

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值