C/C++ 中的 isblank()


isblank() 函数用于检查传递的字符是否为空格符。它本质上是一个空格字符,还考虑制表符 (\t)。此函数在 C 语言中的“ctype.h”头文件中声明,在 C++ 语言的“cctype”头文件中声明。

下面是 C++ 语言中 isblank() 的语法:

int isblank(int char);

下面是 C++ 语言中 isblank() 的示例:

示例

 在线演示

#include <ctype.h>
#include <iostream>
using namespace std;
int main() {
   string s = "The space between words. ";
   int i = 0;
   int count = 0;
   while(s[i]) {
      char c = s[i++];
      if (isblank(c)) {
         count++;
      }
   }
   cout << "\nNumber of blanks in sentence : " << count << endl;
   return 0;
}

输出

Number of blanks in sentence : 4

在上面的程序中,一个字符串传递在变量 s 中。isblank() 函数用于检查所传递的字符串中的空格或空白,如下面的代码片段所示。

string s = "The space between words. ";
int i = 0;
int count = 0;
while(s[i]) {
   char c = s[i++];
   if (isblank(c)) {
      count++;
   }
}

更新日期:2020-06-26

653 次浏览

开启职业生涯之路

通过完成课程获得认证

开始学习
广告
© . All rights reserved.