曲径通幽论坛

标题: isspace() -- 测试空白字符 [打印本页]

作者: beyes    时间: 2011-12-17 12:58
标题: isspace() -- 测试空白字符
isspace() 函数原型如下:
[C++] 纯文本查看 复制代码
#include <ctype.h>
int isspace(int c);

该函数检查和参数 c 是否为空白字符。空白字符包括:空格(' '),制表符('\t'),回车('\r'),换行('\n') ,垂直制表符('\v') 以及换页 ('\f') 。该函数也是个宏函数。

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

int main()
{
        char str[] = "1 2\t345\nabc\rdef\ngh\v";
        int i;

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

        return 0;
}

运行输出:
./isspace
str[1] is a whitespace character: 32
str[3] is a whitespace character: 9
str[7] is a whitespace character: 10
str[11] is a whitespace character: 13
str[15] is a whitespace character: 10
str[18] is a whitespace character: 11
上面程序打印出了这些空白符的 ASCII 码。




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