LinuxC语言字符函数
/* * File: main.cpp * Author: chenqiang * * Created on April 1, 2011, 10:40 AM */ #include<ctype.h> #include<stdio.h> int main() { // freopen("out", "w", stdout); char str2[] = "123c @# FD\tsP[e?*\n"; int i; //isalnum(int i)测试字符是否为英文字母或数字 for (i = 0; str2[i] != '\0'; i++) { if (isalnum(str2[i])) { printf("%c is an alphanumeric character\n", str2[i]); } } printf("\n"); //isalpha(int i)测试字符是否为英文字母 for (i = 0; str2[i] != '\0'; i++) { if (isalpha(str2[i])) { printf("%c is an alphabetic character\n", str2[i]); } } printf("\n"); //isascii(int i)测试字符是否为ascii码字符 for (i = 125; i < 130; i++) { if (isascii(i)) { printf("%d is an ascii character: %c\n", i, i); } else { printf("%d is not an ascii character\n", i); } } printf("\n"); //isblank(int i)测试字符是否为空格或tab键 for (i = 0; str2[i] != '\0'; i++) { if (isblank(str2[i])) { printf("str2[%d] is blank character:%d\n", i, str2[i]); } } printf("\n"); //iscntrl(int i)测试字符是否为控制字符,ascii码小于32的字符 for (i = 0; str2[i] != '\0'; i++) { if (iscntrl(str2[i])) { printf("str2[%d] is a control character:%d\n", i, str2[i]); } } printf("\n"); //isdigit(int i)测试字符是否是数字字符 for (i = 0; str2[i] != '\0'; i++) { if (isdigit(str2[i])) { printf("str2[%d] is a digit character:%c\n", i, str2[i]); } } printf("\n"); //isgraph(int i)测试字符是否是可打印字符,不包过空格 for (i = 0; str2[i] != '\0'; i++) { if (isgraph(str2[i])) { printf("str2[%d] is a printable character:%c\n", i, str2[i]); } } printf("\n"); //islower(int i)测试字符是否为英文小写字母 for (i = 0; str2[i] != '\0'; i++) { if (islower(str2[i])) { printf("%c is a lower character\n", str2[i]); } } printf("\n"); //isprint(int i)测试字符是否是可打印字符,包括空格 for (i = 0; str2[i] != '\0'; i++) { if (isprint(str2[i])) { printf("str2[%d] is a printable character:%c\n", i, str2[i]); } } printf("\n"); //isspace(int i)测试字符是否是空格(‘ ’),定位符('\t'),CR('\r'),换行('\n'),垂直定位符('\v),换页(‘\f') for (i = 0; str2[i] != '\0'; i++) { if (isspace(str2[i])) { printf("str2[%d] is a white-space character:%d\n", i, str2[i]); } } printf("\n"); //ispunct(int i)测试字符是否为标点符号或特殊符号 for (i = 0; str2[i] != '\0'; i++) { if (ispunct(str2[i])) { printf("%c is a punct character\n", str2[i]); } } printf("\n"); //isupper(int i)测试字符是否是英文大写字母 for (i = 0; str2[i] != '\0'; i++) { if (isupper(str2[i])) { printf("%c is a upper character\n", str2[i]); } } printf("\n"); //isxdigit(int i)测试字符是否为16进制数字 for (i = 0; str2[i] != '\0'; i++) { if (isxdigit(str2[i])) { printf("%c is a hexadecimal digit\n", str2[i]); } } // fclose(stdout); }
posted on 2011-04-02 11:42 chenqiangjsj 阅读(235) 评论(0) 编辑 收藏 举报