Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình C

Hàm isblank() trong C/C++

Hàm isblank() kiểm tra ký tự (char) có phải blank (space ' ' hoặc tab '\t') không. Khai báo trong <ctype.h> (C) / <cctype> (C++).

Cú pháp

int isblank(int c);

Ví dụ C++

#include <iostream>
#include <cctype>
using namespace std;

int main() {
    string s = "The space between words. ";
    int count = 0;
    
    for (char c : s) {
        if (isblank(c))
            count++;
    }
    cout << "So ky tu blank: " << count << endl;
    return 0;
}

Kết quả

So ky tu blank: 4

Giải thích: Chuỗi "The space between words. " có 4 khoảng trắng (space). Ký tự cuối cũng là space. isblank không đếm newline '\n'.

Lưu ý:
  • isblank khác isspace: isspace bao gồm space, tab, newline, vertical tab, form feed, carriage return.
  • C++ locale-aware: std::isblank(c, loc).
  • C: cần ép (unsigned char)c cho an toàn.