2020-11-05

## C语言:输入一行字符,分别统计其中英文字母,空格,数字和其他数字的个数

  1. 
    //直接字符查找 
    #include <stdio.h>
    #include <string.h>
    int main(){
    	int num=0;
    	int letter=0;
    	int space=0;
    	int others=0;
    	
    	char string[99];
    	gets(string);
    	int l=strlen(string);
    	for(int i;i<l;i++){
    		char temp=string[i];
    		if(temp>='0' && temp<='9') num++;
    		else if((temp>='a' && temp<='z')||(temp>='A' && temp<='Z')) letter ++;
    		else if(temp==' ') space++;
    		else others++;
    	}
    	printf("num=%d	,letter=%d	,space=%d	,others=%d",num,letter,space,others);
    	return 0;
    } 

 第二种方法

  1. //ASCII 码
    #include <stdio.h>
    #include <string.h> 
    int main(){
    	int num=0;  //数字个数
    	int letter=0;  //字母个数
    	int space=0;  //空格个数
    	int others=0; //其他
    	
    	char str[100];
    	gets(str);   //之前用的scanf("%s",str);在空格之后,无法判断之后的  
    
    	//int l=strlen(str);  //在空格之后的字符无法测出
    	char c;
    	for(int i=0;(c=str[i]!='\0');i++){  //只要字符不是'\0',继续执行 
    		int temp=str[i];
    		if(temp>=48 && temp<=57) num++;
    		else if((temp>=65 && temp<=90) || (temp>=97 && temp<=122))letter++;
    		else if(temp==32) space++;
    		else others++;
    	} 
    	printf("num=%d ,letter=%d ,space=%d ,others=%d",num,letter,space,others);
    	return 0;
    } 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值