#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);
}
9.14.1-PointersOnC-20220301
于 2022-03-01 13:31:08 首次发布