zoukankan      html  css  js  c++  java
  • 输入一行文字,找出其中大写字母、小写字母、空格、数字以及其他字符各有多少

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

    解题思路: 字符可以直接进行比较,但是要注意字符串中的数字是字符数字,必须以字符的形式比较,也就是加上单引号

    答案:

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	char buf[1024];
    	printf("Please enter a string: ");
    	gets_s(buf, 1024);
    	int upper_count = 0, lower_count = 0, digit_count = 0, space_count = 0, other_count = 0;
    	char *ptr = buf;
    	while (*ptr != '') {
    		if (*ptr >= 'A' && *ptr <= 'Z') { //大写字母
    			upper_count++;
    		}else if (*ptr >= 'a' && *ptr <= 'z'){//小写字母
    			lower_count++;
    		}else if (*ptr >= '0' && *ptr <= '9') {//数字字符
    			digit_count++;
    		}else if (*ptr== ' ') {//空格字符
    			space_count++;
    		}else { //其他字符
    			other_count++;
    		}
    		ptr++;
    	}
    	printf("upper:%d; lower:%d; digit:%d; space:%d; other:%d
    ", 
    		upper_count, lower_count, digit_count, space_count, other_count);
    	system("pause");
    	return 0;
    }
    
    

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

  • 相关阅读:
    股票
    使用广播信道的以太网
    CSMA/CD 3
    可赎回债券
    matlab中的knn函数
    债券 账面值
    最优化作业 共轭梯度法 matlab代码
    债券和股票 溢价公式
    债券和股票
    CSMA/CD 续
  • 原文地址:https://www.cnblogs.com/cyuyanchengxu/p/13386182.html
Copyright © 2011-2022 走看看