曲径通幽论坛

标题: isgraph() -- 测试字符是否为可打印字符 [打印本页]

作者: beyes    时间: 2011-12-17 09:16
标题: isgraph() -- 测试字符是否为可打印字符
isgraph() 函数原型如下:
[C++] 纯文本查看 复制代码
#include <ctype.h>
int isgraph(int c);

该函数测试参数 c 是否为可打印字符,如果 c 对应于 ASCII 中可打印且非空白字符,那么返回 TRUE,否则返回 NULL 。该函数也是个宏函数。

测试代码
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <ctype.h>

int main()
{
        char str[] = "12 ab !   #";
        int i;

        for (i = 0; str != 0; i++)
                if (isgraph(str))
                        printf("str[%d] is printable character:%c\n", i, str);

        return 0;
}

运行输出:
./isgraph
str[0] is printable character:1
str[1] is printable character:2
str[3] is printable character:a
str[4] is printable character:b
str[6] is printable character:!
str[8] is printable character:#
上面的 str[] 数组中有一个空格和一个 TAB ,它们都属于不可打印字符。
作者: beyes    时间: 2011-12-17 13:32
标题: isprint() -- 测试打印字符
isprint() 也可用来测试字符是否是可打印字符,其原型如下:
[C++] 纯文本查看 复制代码
#include <ctype.h>
int isprint(int c);

和上面的 isgraph() 一样检测参数 c ,如果是可打印字符(包括空白格符)则返回 TRUE,否则返回 NULL 。

测试代码
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <ctype.h>

int main()
{
        char str[] = "a b       de\\nld\\v";
        int i;


        for (i = 0; str != 0; i++)
                if (isprint(str))
                        printf("str[%d] is printable character:%d\\n", i, str);

        return 0;
}

运行输出:
./isprint
str[0] is printable character:97
str[1] is printable character:32
str[2] is printable character:98
str[4] is printable character:100
str[5] is printable character:101
str[7] is printable character:108
str[8] is printable character:100
程序中,在 b 和 d 之间是直接用 TAB 键敲出的一个制表符。isprint() 函数认为空格符是可打印字符,TAB 的制表符,以及换行,垂直制表符都非可打印字符。




欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2