方法一
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define M 100 //变量定义 M = 100
int bigLetter; //全局变量 大写字母
int smallLetter; //全局变量 小写字母
int space; //全局变量 空格
int number; //全局变量 数字
int Other; //全局变量 其他字符
void str(char *p,int c);
main()
{
char q1[M];
int c; //用来计算字符长度
printf("请输入一串字符串里面包括大小写字母,空格,数字,其他字符:");
gets(q1);
c=strlen(q1);
printf("\n字符串总长度为:%d\n",c); //可以判断是否相等
str(q1,c);
}
void str(char *p,int c)
{
for(int i=0;i<c;i++)
{
if( 'A' <=*(p+i) && *(p+i)<= 'Z' ) //'A'= 65, 'Z'=90
bigLetter++;
if( 'a' <=*(p+i) && *(p+i)<= 'z' ) //'a'=97 , 'z'=122
smallLetter++;
if( '0' <=*(p+i) && *(p+i)<= '9' ) //'0'=48', '9'=57
number++;
if(*(p+i)==32) //空格ASCLL码值为 32
space++;
if(33 <= *(p+i) && *(p+i)<= 47 || 58 <= *(p+i) && *(p+i) <= 64 || 91<=*(p+i) && *(p+i) <= 96 || 123 <= *(p+i) && *(p+i) <= 126)
Other++;
}
printf("大写字母=%d\n小写字母=%d\n数字=%d\n空格=%d\n其他字符=%d\n",bigLetter,smallLetter,number,space,Other);
}
方法二:
#include<stdio.h>
#include<string.h> //使用strlen()函数时要加
#include<conio.h> //使用gets()函数时要加
#include<ctype.h> //使用字符函数时要加的头文件,列isupper(),islower()等等
#define M 100
int bigLetter; //全局变量 大写字母
int smallLetter; //全局变量 小写字母
int space; //全局变量 空格
int number; //全局变量 数字
int Other; //全局变量 其他字符
main()
{
char q1[M];
int c;
printf("请输入一串字符串里面包括大小写字母,空格,数字,其他字符:");
gets(q1);
c=strlen(q1);
printf("\n字符串总长度为:%d\n",c);
for(int i=0;i<c;i++)
{
if(isupper(q1[i])!=0) //函数isupper()检查到是大写字母,就会返回非0,不是就返回0
bigLetter++;
if(islower(q1[i])!=0) //函数silower()检查到是小写字母,就会返回非0,不是就返回0
smallLetter++;
if(isdigit(q1[i])!=0) //函数isdigit()检查到是数字,就会返回非0,不是就返回0
number++;
if(isspace(q1[i])!=0) //函数isspace()检查到是空格,就会返回非0,不是就返回0
space++;
if(ispunct(q1[i])!=0) //函数ispunct()检查到是其他字符,就会返回非0,不是就返回0
Other++;
}
printf("大写字母=%d\n小写字母=%d\n数字=%d\n空格=%d\n其他字符=%d\n",bigLetter,smallLetter,number,space,Other);
}
*/
方法三:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define M 100
main()
{
char q1[M];
int bigLetter=0; //大写字母
int smallLetter=0; //小写字母
int space=0; //空格
int number=0; //数字
int Other=0; //其他字符
printf("请输入一串字符串里面包括大小写字母,空格,数字,其他字符:");
gets(q1);
printf("\n字符串的长度为=%d\n",strlen(q1));
for(int i=0;i<strlen(q1);i++)
{
if(65<=q1[i]&&q1[i]<=90) //'A'= 65, 'Z'=90
bigLetter++;
if(97<=q1[i]&&q1[i]<=122) //'a'=97 , 'z'=122
smallLetter++;
if(48<=q1[i]&&q1[i]<=57) //'0'=48', '9'=57
number++;
if(q1[i]==32)
space++;
if(33 <= q1[i] && q1[i]<= 47 || 58 <= q1[i] && q1[i] <= 64 || 91<=q1[i] &&q1[i] <= 96 || 123 <= q1[i] && q1[i] <= 126)
Other++;
}
printf("大写字母=%d\n小写字母=%d\n数字=%d\n空格=%d\n其他字符=%d\n",bigLetter,smallLetter,number,space,Other);
}