9.14.1-PointersOnC-20220301

#include<stdio.h>
#include<string.h>
#include<ctype.h>
typedef struct{
	int cntrl;
	int space;
	int digit;
	int lower;
	int upper;
	int punct;
	int nonprint;
	char str[101];
	int count;
}Data;

char *my_fgets(char *p,int n);
void ShowCount(Data *data);
int main(){
	Data data={0};
	int total=0;
	char *pstr=NULL;
	for(fputs("Input the string:->",stdout);(pstr=my_fgets(data.str,101))&&*pstr;ShowCount(&data),fputs("Input the string:->",stdout));
return 0;}
//
char *my_fgets(char *p,int n){
	char ch=0,*input=NULL,*find=NULL;
	if(input=fgets(p,n,stdin))
		if(find=strchr(input,10))
			*find=0;//将'\n'改成0
		else
			while((ch=getchar())!=EOF&&ch!=10);//清空缓存
return input;}

void ShowCount(Data *data){
	Data data_temp={0};
	for(char *pstr=data->str;*pstr;pstr++){
		data->count++,data_temp.count++;
		if(iscntrl(*pstr)) {data->cntrl++;data_temp.cntrl++;continue;}
		if(isspace(*pstr)) {data->space++;data_temp.space++;continue;}
		if(isdigit(*pstr)) {data->digit++;data_temp.digit++;continue;}
		if(islower(*pstr)) {data->lower++;data_temp.lower++;continue;}
		if(isupper(*pstr)) {data->upper++;data_temp.upper++;continue;}
		if(ispunct(*pstr)) {data->punct++;data_temp.punct++;continue;}
		if(!isprint(*pstr)) {data->nonprint++;data_temp.nonprint++;continue;}
	}
	printf("The string \"%s\" contains:\ncntrl->%d(%0.2f%%)\nspace->%d(%0.2f%%)\ndigit->%d(%0.2f%%)\nlower->%d(%0.2f%%)\nupper->%d(%0.2f%%)\npunct->%d(%0.2f%%)\nnonprint->%d(%0.2f%%)\n",
	data->str,
	data_temp.cntrl,data_temp.cntrl*100.0/data_temp.count,data_temp.space,data_temp.space*100.0/data_temp.count,data_temp.digit,data_temp.digit*100.0/data_temp.count,
	data_temp.lower,data_temp.lower*100.0/data_temp.count,data_temp.upper,data_temp.upper*100.0/data_temp.count,data_temp.punct,data_temp.punct*100.0/data_temp.count,
	data_temp.nonprint,data_temp.nonprint*100.0/data_temp.count);
	printf("Database contains:\ncntrl->%d(%0.2f%%)\nspace->%d(%0.2f%%)\ndigit->%d(%0.2f%%)\nlower->%d(%0.2f%%)\nupper->%d(%0.2f%%)\npunct->%d(%0.2f%%)\nnonprint->%d(%0.2f%%)\n\n",
	data->cntrl,data->cntrl*100.0/data->count,data->space,data->space*100.0/data->count,data->digit,data->digit*100.0/data->count,
	data->lower,data->lower*100.0/data->count,data->upper,data->upper*100.0/data->count,data->punct,data->punct*100.0/data->count,
	data->nonprint,data->nonprint*100.0/data->count);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fleet1126

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值