C++ 中的 isprint() 函式
C++ 中的 isprint() 是 “cctype.h” 標頭檔案中內建的函式,用於檢查字元是否可列印。
除了空格字元 (' ') 外,isprint() 函式對於常量字元返回 true。
cctype 標頭檔案中存在此函式 (isprint()) 的特定區域設定版本。
- isprint() 函式可用於檢查一系列句子中的任何非列印字元。
- isprint() 是一個內建函式,提供了一種高效處理非列印字元的方法。
- isprint() 有助於減少程式設計師的程式碼行數。
- isprint() 真正意義上減少了程式的編譯時間。
在程式中包含 cctype.h 不僅允許使用者使用 isprint(),還解鎖了許多其他相關函式。cctype.h 中包含的一些其他函式是:
- isblank (檢查字元是否為空格)
- iscntrl (檢查字元是否為控制字元)
- isdigit (檢查字元是否為十進位制數字)
- isgraph (檢查字元是否具有圖形表示)
語法
isprint() 的語法如下:
Int isprint (int c);
“可列印字元是指在顯示器上佔據列印位置的字元”。
isprint() 的引數是:
C 是要檢查的字元,轉換為 int 或 EOF。
示例
Input-: first line /n second line /n Output-: first line Input-: line one /n line two/n line three /n Output-: line one
說明:它只會列印一行,因為換行符不可列印。
示例
/* isprint example */
#include <stdio.h>
#include <ctype.h>
int main () {
int i=0;
char str[]="first line n second line n";
while (isprint(str[i])) {
putchar (str[i]);
i++;
}
return 0;
}輸出
如果執行上述程式碼,將生成以下輸出:
first line n second line n
示例
#include <cctype>
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "Hellotallnhow are you";
for (int i=0; i<strlen(str); i++) {
if (!isprint(str[i]))
str[i] = ' ';
}
cout << str;
return 0;
}輸出
如果執行上述程式碼,將生成以下輸出:
Hellotallnhow are you
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP