ในบทความนี้ เราจะพูดถึงฟังก์ชัน iswspace() ใน C++, ไวยากรณ์, การทำงาน และค่าส่งคืน
ฟังก์ชัน iswspace() เป็นฟังก์ชัน inbuilt ใน C++ ซึ่งกำหนดไว้ในไฟล์ส่วนหัว ฟังก์ชันตรวจสอบว่าอักขระที่ส่งผ่านแบบกว้างนั้นเป็นอักขระช่องว่างหรือไม่ ฟังก์ชันนี้เป็นอักขระแบบกว้างที่เทียบเท่ากับ isspace() ซึ่งหมายความว่าใช้งานได้เหมือนกับ isspace() ความแตกต่างคือรองรับอักขระแบบกว้าง ฟังก์ชันตรวจสอบว่าอาร์กิวเมนต์ที่ส่งผ่านเป็นช่องว่าง (' ') แล้วคืนค่าจำนวนเต็มที่ไม่เป็นศูนย์ (จริง) หรือไม่ มิฉะนั้นจะคืนค่าศูนย์ (เท็จ)
ไวยากรณ์
int iswspace(wint_t ch);
ฟังก์ชันยอมรับพารามิเตอร์เพียงตัวเดียว กล่าวคือ อักขระแบบกว้างที่ต้องตรวจสอบ อาร์กิวเมนต์อยู่ใน wint_t หรือ WEOF
wint_t เก็บข้อมูลประเภทหนึ่ง
คืนค่า
ฟังก์ชันส่งคืนค่าจำนวนเต็ม ซึ่งสามารถเป็น 0 (ในกรณีที่เป็นเท็จ) หรือค่าที่ไม่ใช่ศูนย์ใดๆ (ในกรณีที่เป็นจริง)
ตัวอย่าง
#include <iostream>
#include <cwctype>
using namespace std;
int main() {
wint_t a = '.';
wint_t b = ' ';
wint_t c = '1';
iswspace(a)?cout<<"\nIts white space character":cout<<"\nNot white space character";
iswspace(b)?cout<<"\nIts white space character":cout<<"\nNot white space character";
iswspace(c)?cout<<"\nIts white space character":cout<<"\nNot white space character";
} ผลลัพธ์
หากเราเรียกใช้โค้ดข้างต้น มันจะสร้างผลลัพธ์ต่อไปนี้ -
Not white space character Its white space character Not white space character
ตัวอย่าง
#include <iostream>
#include <cwctype>
using namespace std;
int main () {
int i, count;
wchar_t s[] = L"I am visiting tutorials point";
count = i = 0;
while (s[i]) {
if(iswspace(s[i]))
count++;
i++;
}
cout<<"There are "<<count <<" white space characters.\n";
return 0;
} ผลลัพธ์
หากเราเรียกใช้โค้ดข้างต้น มันจะสร้างผลลัพธ์ต่อไปนี้ -
There are 4 white space characters.