c - 如何过滤掉 ASCII 中“Z”和“a”之间的垃圾?
问题描述
#include <stdio.h>
void main()
{
char ch;
clrscr();
ch = 'A';
while(ch <= 'z')
{
printf("%c", ch);
ch++;
}
getch();
}
如何从该程序的输出中删除诸如[
, ]
, '
,等的垃圾?\
解决方案
就像评论中提到的那样跳过 Z 之间的所有内容,例如:
#include "stdio.h"
int main(){
char ch = 'A';
while(ch <= 'z'){
if(ch <= 'Z' || ch >= 'a'){
printf("%c", ch);
}
ch++;
}
printf("\n");
return 0;
}
或者使用for
循环,因为您知道值范围的开始和结束:
#include "stdio.h"
int main(){
char ch;
for(ch = 'A'; ch <= 'z'; ch++){
// loop through range of ASCII values from 'A' to 'z', upper case
// and lower case letters, and print out only alphabetic characters.
if(ch <= 'Z' || ch >= 'a'){
// character is in range of 'A' thru 'Z' or 'a' thru 'z'.
printf("%c", ch);
}
}
printf("\n");
return 0;
}
或者使用该isalpha()
函数来检测字符是否为字母。
#include "stdio.h"
int main() {
char ch;
// loop through range of ASCII values from 'A' to 'z', upper case
// and lower case letters, and print out only alphabetic characters.
for (ch = 'A'; ch <= 'z'; ch++) isalpha (ch) && printf ("%c", ch);
printf("\n");
return 0;
}
推荐阅读
- google-chrome - Chrome 浏览器无限控制台记录 MessageEvent
- php - Google Indexing API 仅更新指定国家/地区的 URL
- javascript - imgur 未使用 ckeditor 加载
- virtual-machine - 将虚拟机从 ESX 复制到本地虚拟机播放器
- python - 从 sqlite3 数据库中检索图像以进行人脸验证
- android - 如何管理 Android 和 IOS 应用程序的部署、安装、升级以供移动设备内部使用?
- symfony - 使用左连接查询 symfony 5 中的相关实体
- c# - 有没有办法在 join/linq 中使用成员表达式?反射
- node.js - 用 Next JS 设置 Supervisor 的正确方法
- mongodb - MongoDB:在一定时间后将字段值重置为默认值(Date.now() + x)