跳到主要内容

C++ isblank() 判断字符是否为空白

isblank() 函数原型

int isblank(int ch);

isblank() 函数用于检查 ch 是否是空白字符,这是基于当前安装的 C 语言区域设置。默认情况下,空格和水平制表符被视为空白字符。

如果 ch 的值无法表示为无符号字符或不等于 EOF,则 isblank() 的行为是未定义的。

它定义在 <cctype> 头文件中。

isblank() 参数

ch:要检查的字符。

isblank() 返回值

如果 ch 是一个空白字符,isblank() 函数返回非零值;否则返回零。

示例:isblank() 函数如何工作

#include <cctype>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
char str[] = "Hello, I am here.";
int count = 0;

for (int i = 0; i <= strlen(str); i++)
{
if (isblank(str[i]))
count ++;
}

cout << "Number of blank characters: " << count << endl;

return 0;
}

当你运行这个程序时,输出将会是:

Number of blank characters: 3