本文已参与「新人创作礼」活动,一起开启掘金创作之路。
1 统计一行字符中各类字符的个数
用户输入一行字符(以回车符作为结束),请统计这行字符中,大小写字母个数、数字字符个数、空格个数和其它字符个数。
输入格式:
一行字符,以回车符作为结束。
输出格式:
输出4个以空格作为间隔的整数,分别代表大小写字母个数、数字字符个数、空格个数和其它字符的个数。
输入样例:
Tersfi23&* sdf A$
输出样例:
10 2 4 3
我的代码
#include<stdio.h>
int main()
{
int c=0,num=0,space=0,other=0;
char ch;
while((ch=getchar())!='\n')
{
if(ch<='z'&&ch>='a'||ch<='Z'&&ch>='A')
{
c++;
}
else if(ch<='9'&&ch>='0')
{
num++;
}
else if(ch==' ')
{
space++;
}
else
{
other++;
}
}
printf("%d %d %d %d",c,num,space,other);
}
2 统计一个整数的位数
本题要求编写程序,对于给定的整数N,求它的位数。
输入格式:
输入在一行中给出一个绝对值不超过109的整数N。
输出格式:
在一行中输出N的位数。
输入样例1:
12534
输出样例1:
5
输入样例2:
-987600321
输出样例2:
9
输入样例3:
0
输出样例3:
1
我的代码
#include<stdio.h>
int main()
{
int n,count;
scanf("%d",&n);
if(n<0)
{
n=-n;
}
do
{
count++;
n=n/10;
}while(n!=0);
printf("%d",count);
return 0;
}