输入一行文字,找出其中大写字母、小写字母、空格、数字、以及其他字符各有多少。

方法一

#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);
}

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值