Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ฟังก์ชัน isprint() ใน C++

ฟังก์ชัน isprint() เป็นฟังก์ชันสำเร็จรูปที่ตรวจสอบว่าตัวอักษรที่ส่งเข้ามาเป็นตัวอักษรที่พิมพ์ได้ (printable) หรือไม่ ส่งคืนค่าที่ไม่ใช่ศูนย์หากสำเร็จ มิฉะนั้นส่งคืนศูนย์ ฟังก์ชันนี้ประกาศอยู่ในไฟล์หัวข้อ <cctype>

รูปแบบการใช้งาน

int isprint(int character);

character − ตัวอักษรที่ต้องการตรวจสอบ

ตัวอย่างโค้ด

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

int main() {
    int val1 = 28;
    int val2 = 's';
    int val3 = '\n';
    if(isprint(val1))
        cout << "value is printable" << endl;
    else
        cout << "value is not printable" << endl;
    if(isprint(val2))
        cout << "value is printable" << endl;
    else
        cout << "value is not printable" << endl;
    if(isprint(val3))
        cout << "value is printable" << endl;
    else
        cout << "value is not printable" << endl;
    return 0;
}

ผลลัพธ์

value is not printable
value is printable
value is not printable

ในโปรแกรมนี้ ประกาศตัวแปร 3 ตัวคือ val1, val2, val3 ตรวจสอบแต่ละตัวว่าพิมพ์ได้หรือไม่