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

发布时间 2023-10-17 19:45:53作者: Bonne_chance

输入一行字符,分别统计其中中英文字母、空格、数字和其他字符的个数

 #include <stdio.h>
 //输入一行字符,分别统计其中中英文字母、空格、数字和其他字符的个数
 int main(){
 	char c;
 	int letters = 0;
 	int spaces = 0;
 	int digits = 0;
 	int others = 0;
 	printf("input  some characteristics:\n");
 	while ((c = getchar())!= '\n'){
 		if((c >= 'a' && c <= 'z')||(c >= 'A' && c <= 'Z')){
 			letters ++; //字母
		 }
		else if (c >= '0' && c <= '9'){
			digits ++;//数字
		}
		else if (c == ' '){
			spaces ++;//空格
		}
		else{
			others ++;//其他
		}
 		
 		
	 }
	printf("letters = %d, digits = %d, spaces = %d, others = %d\n", letters, digits, spaces, others);
	return 0;
 	
 	
 }

结果: