#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int proc(char ch);
void f(char str[],int *p1,int *p2,int *p3,int *p4);
int main(void)
{
int num1 = 0;
//记录大写字母的个数
int num2 = 0;
//记录空格的个数
int num3 = 0;
//记录数字的个数
int num4 = 0;
//记录其他字符的个数
int len;
char *str = NULL;
printf("Please input the string's length: ");
scanf("%d",&len);
printf("len = %d\n",len);
while(getchar() != '\n')
continue;
str = (char *)malloc(len*sizeof(char));
if(NULL == str)
{
puts("Memory allocation failed.Goodbye!");
exit(EXIT_FAILURE);
}
printf("Please input the string:\n");
fgets(str,len,stdin);
f(str,&num1,&num2,&num3,&num4);
printf("在%s里,大写字母的个为%d,空格的个数为%d,数字的个数为%d,其他字符的个数为%d!\n",str,num1,num2,num3,num4);
free(str);
system("pause");
return 0;
}
int proc(char ch)
{
if(isupper(ch))
return 0;
else
if(' ' == ch)
return 1;
else
if(isdigit(ch))
return 2;
else
return 3;
}
void f(char str[],int *p1,int *p2,int *p3,int *p4)
{
int i;
int len = strlen(str);
for(i = 0;i < len;i++)
{
switch(proc(str[i]))
{
case 0:
(*p1)++;
break;
case 1:
(*p2)++;
break;
case 2:
(*p3)++;
break;
case 3:
(*p4)++;
break;
}
}
}