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

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

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

答案:

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

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

原文地址:https://www.cnblogs.com/weiyidedaan/p/13275382.html