当前位置:网站首页>PTA习题 分类统计字符个数(C)
PTA习题 分类统计字符个数(C)
2022-08-09 10:58:00 【拾荒大妈】
本题要求实现一个函数,统计给定字符串中英文字母、空格或回车、数字字符和其他字符的个数。
函数接口定义:
void StringCount( char s[] );
其中 char s[] 是用户传入的字符串。函数StringCount须在一行内按照
| letter = 英文字母个数, blank = 空格或回车个数, digit = 数字字符个数, other = 其他字符个数的格式输出。 | |
|---|---|
裁判测试程序样例:
#define MAXS 15
void StringCount( char s[] );
void ReadString( char s[] ); /* 由裁判实现,略去不表 */
int main()
{
char s[MAXS];
ReadString(s);
StringCount(s);
return 0;
}
/* Your function will be put here */
在这里插入代码片
输入样例:
aZ &
09 Az
输出样例:
letter = 4, blank = 3, digit = 2, other = 1
思路
通过使用strlen首先判断字符串中的字符个数,然后依次判断字符串中字符的类型。(也可以通过s[i]==’\0’来判断字符串是否已经结束)
void StringCount( char s[] )
{
int letter=0;
int blank=0,digit=0,other=0;
int n=strlen(s);
int i=0;
for(;i<n;i++)
{
if((s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z'))
letter++;
else if(s[i]>='0'&&s[i]<='9') digit++;
else if(s[i]==' '||s[i]=='\n')blank++;
else other++;
}
printf("letter = %d, blank = %d, digit = %d, other = %d",letter,blank,digit, other);
}

做题时遇到的问题
1.应该是’A’,而自己做题时写成"A"
2. 题目中blank的个数是空格和回车的个数之和
边栏推荐
- 解决1.tensorflow运行使用CPU不使用GPU 2.tensorflow环境下的GPU版本号 3.tensorflow和cuda以及cudnn版本对应问题 4.查看cuda和cudnn版本
- golang interface “坑记录“
- MATLAB代码实现三次样条插值
- activemq 消息持久化
- matlab图像分割,从基因芯片荧光图像中提取阴性点(弱)和阳性点(强)
- 我用开天平台做了一个定时发送天气预报系统【开天aPaaS大作战】
- Shell script combat (2nd edition) / People's Posts and Telecommunications Press Script 1 Find programs in the PATH
- OpenSSF's open source software risk assessment tool: Scorecards
- For versions corresponding to tensorflow and numpy, report FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecate
- 985毕业,工作3年,分享从阿里辞职到了国企的一路辛酸和经验
猜你喜欢
随机推荐
CAN总线发送数据
1006 Sign In and Sign Out (25分)
margin出bug---margin失效
[Original] Usage of @PrePersist and @PreUpdate in JPA
golang runtime Caller、Callers、CallersFrames、FuncForPC、Stack作用
性能测试(05)-表达式和业务关联-json关联
遇到恶意退款不用怕,App 内购买项目的退款通知现已可用
研发需求的验收标准应该怎么写? | 敏捷实践
activemq message persistence
pip common commands and changing source files
CSDN的markdown编辑器语法完整大全
STM32使用静态队列保存数据
Qt获取EXE可执行文件的上一级目录下的文件
我用开天平台做了一个定时发送天气预报系统【开天aPaaS大作战】
faster-rcnn中的RPN原理
TensorFlow:NameError: name ‘input_data’ is not defined
解决1.tensorflow运行使用CPU不使用GPU 2.tensorflow环境下的GPU版本号 3.tensorflow和cuda以及cudnn版本对应问题 4.查看cuda和cudnn版本
MDK添加注释模板
GOPROXY 中国代理
全网最简单解决OneNote中英字体不统一









