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 次瀏覽

開啟職業生涯之路

透過完成課程獲得認證

開始學習
廣告