C语言判断一字符串中只有汉字或者英文字母的方法

代码如下:

include <stdio.h> #include <windows.h> bool isCHNENG(char *str1) { unsigned int temp,len,num_char=0,num_chin=0; len=strlen(str1); temp=(unsigned int)*str1; while(temp) { if(!num_chin&&(temp>='a'&&temp<='z')||(temp>='A'&&temp<='Z')) num_char=1; else if(!num_char&&(temp>0xa0)) num_chin=1; else return false; str1++; temp=(unsigned int)*str1; } return true; } int main(void) { char letter[]="I am chinese"; if(isCHNENG(letter)) //测试 printf("ok, letter! "); else printf("NO WAY, letter! "); return 0; }

comments powered by Disqus