C ++使用isprint()
C++中的Isprint()是“cctype.h”头文件中的内置函数,该头文件检查字符是否可打印。
对于常态,Isprint返回true,因为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