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

ฟังก์ชัน ispunct() ในภาษา C


ฟังก์ชัน ispunct() ใช้สำหรับตรวจสอบว่าตัวอักษรที่ส่งเข้ามาเป็นเครื่องหมายวรรคตอน (punctuation) หรือไม่ ฟังก์ชันนี้คืนค่า 0 หากไม่ใช่เครื่องหมายวรรคตอน มิฉะนั้นจะคืนค่าที่ไม่ใช่ 0 (non-zero)

ไวยากรณ์

int ispunct(int character);

ตัวอย่างการใช้งาน

#include <stdio.h>
#include <ctype.h>

int main() {
    int a = '!';
    int b = 'a';
    
    if (ispunct(a))
        printf("The character is a punctuation.");
    else
        printf("\nThe character is not a punctuation.");
    
    if (ispunct(b))
        printf("\nThe character is a punctuation.");
    else
        printf("\nThe character is not a punctuation.");
    
    return 0;
}

ผลลัพธ์

The character is a punctuation.
The character is not a punctuation.

หมายเหตุ: ฟังก์ชัน ispunct() ต้องรวมไฟล์เฮดเดอร์ <ctype.h> เครื่องหมายวรรคตอนที่ตรวจสอบได้รวมถึง: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~