- C標準庫
- C庫 - 首頁
- C庫 - <assert.h>
- C庫 - <complex.h>
- C庫 - <ctype.h>
- C庫 - <errno.h>
- C庫 - <fenv.h>
- C庫 - <float.h>
- C庫 - <inttypes.h>
- C庫 - <iso646.h>
- C庫 - <limits.h>
- C庫 - <locale.h>
- C庫 - <math.h>
- C庫 - <setjmp.h>
- C庫 - <signal.h>
- C庫 - <stdalign.h>
- C庫 - <stdarg.h>
- C庫 - <stdbool.h>
- C庫 - <stddef.h>
- C庫 - <stdio.h>
- C庫 - <stdlib.h>
- C庫 - <string.h>
- C庫 - <tgmath.h>
- C庫 - <time.h>
- C庫 - <wctype.h>
- C程式設計資源
- C程式設計 - 教程
- C - 有用資源
C庫 - iswspace() 函式
C 的wctype庫 iswspace() 函式用於檢查給定的寬字元(型別為 wint_t)是否為空格字元。
空格字元包括空格、製表符、換行符以及在各種區域設定中被認為是空格的其他字元。具體來說,這些字元是:
- 空格 (0x20)
- 換頁符 (0x0c)
- 換行符 (0x0a)
- 回車符 (0x0d)
- 水平製表符 (0x09)
- 垂直製表符 (0x0b)
語法
以下是 iswspace() 函式的 C 庫語法:
int iswspace( wint_t ch )
引數
此函式接受一個引數:
-
ch − 要檢查的型別為 'wint_t' 的寬字元。
返回值
如果寬字元為空格字元,則此函式返回非零值;否則返回零。
示例 1
以下是一個基本的 C 示例,演示了 iswspace() 函式的使用。
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
int main(void) {
wchar_t ch = L' ';
// use ternary operator
int is_whitespace = iswspace(ch) ? 1 : 0;
printf("Character '%lc' is%s whitespace character\n", ch, is_whitespace ? "" : " not");
return 0;
}
輸出
以下是輸出:
Character ' ' is whitespace character
示例 2
讓我們建立一個另一個 C 程式,並使用 iswspace() 來驗證陣列的每個元素是否為空格。
#include <locale.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
int main(void) {
// Set the locale to "en_US.utf8"
setlocale(LC_ALL, "en_US.utf8");
// Array of wide characters
wchar_t wchar[] = {
L'X', L' ', L'\n', L'\t', L'@', L'\u00A0', L'\u2003'
};
size_t size = sizeof(wchar) / sizeof(wchar[0]);
printf("Checking whitespace characters:\n");
for (size_t i = 0; i < size; ++i) {
wchar_t ch = wchar[i];
printf("Character '%lc' (U+%04X) is %s whitespace character\n", ch, ch, iswspace(ch) ? "a" : "not a");
}
return 0;
}
輸出
以下是輸出:
Checking whitespace characters: Character 'X' (U+0058) is not a whitespace character Character ' ' (U+0020) is a whitespace character Character ' ' (U+000A) is a whitespace character Character ' ' (U+0009) is a whitespace character Character '@' (U+0040) is not a whitespace character Character ' ' (U+00A0) is not a whitespace character Character ' ' (U+2003) is a whitespace character
示例 3
下面的 C 示例使用 iswspace() 函式在遇到空格後中斷語句。
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
int main(void) {
// wide char array
wchar_t statement[] = L"Tutrialspoint India pvt ltd";
// find the length of wide char
size_t len = wcslen(statement);
for (size_t i = 0; i < len; ++i) {
// Check if the current character is whitespace
if (iswspace(statement[i])) {
// Print a newline character if whitespace is found
putchar('\n');
} else {
// Print the current character if it's not whitespace
putchar(statement[i]);
}
}
return 0;
}
輸出
以下是輸出:
Tutrialspoint India pvt ltd
c_library_wctype_h.htm
廣告