当前位置:网站首页>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的个数是空格和回车的个数之和
边栏推荐
- Getting Started with MNIST Machine Learning
- matlab fcnchk 函数用法
- CAN总线发送数据
- pip common commands and changing source files
- matlab图像分割,从基因芯片荧光图像中提取阴性点(弱)和阳性点(强)
- 聚类了解
- 绝了,这套RESTful API接口设计总结
- Solve 1. tensorflow runs using CPU but not GPU 2. GPU version number in tensorflow environment 3. Correspondence between tensorflow and cuda and cudnn versions 4. Check cuda and cudnn versions
- FreeRTOS列表和列表项源码分析
- 实测办公场景下,国产远程控制软件的表现力如何?(技术解析)
猜你喜欢
随机推荐
MDK添加注释模板
golang interface “坑记录“
Shell script combat (2nd edition) / People's Posts and Telecommunications Press Script 1 Find programs in the PATH
类与对象 (下)
Beauty Values
golang源代码阅读,sync系列-Cond
MATLAB中如何把cftool拟合的函数输出到命令行(解决如何导出拟合后的曲线数据)
PoseNet: A Convolutional Network for Real-Time 6-DOF Camera Relocalization Paper Reading
For versions corresponding to tensorflow and numpy, report FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecate
自从我使用HiFlow场景连接器后,在也不用担心成为“落汤鸡”了
∘(空心的点乘)的数学含义
threejs+shader 曲线点运动,飞线运动
为什么组合优先于继承
leetcode-搜索旋转排序数组-33
1005 Spell It Right (20分)
CAN总线发送数据
prometheus接入mysqld_exporter
Mysql多表查询
Product Quantization (PQ)
2022年台湾省矢量数据(点线面)及数字高程数据下载