统计各类字符个数

这篇博客主要介绍了如何使用Java统计输入的10个字符中,包括英文字母(区分大小写)、空格或回车、数字以及其他字符的个数。内容涉及字符类型的判断和计数方法。

统计各类字符个数

【问题描述】
输入10 个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数。
【输入形式】
从键盘输入正实数x的值。
【输入输出样例1】

Input 10 characters: Reold 123?
letter=5,blank=1,digit=3,other=1

【样例说明】
输入提示符后要加一个空格。例如“Input 10 characters: ”,其中“:”后要加一个且只能一个空格。
输出语句的“=”两边无空格。
英文字母区分大小写。必须严格按样例输入输出。
代码:

import java.util.*;
public class Main {
   
   
    public 
在C语言中统计各类字符个数,通常是统计英文字母空格回车数字字符其他字符个数,以下是几种实现方法: ### 方法一:统计固定数量字符 此方法用于输入10个字符统计其中英文字母空格回车数字字符其他字符个数。 ```c #include <stdio.h> int main() { char ch; int letter = 0, blank = 0, digit = 0, other = 0; for (int i = 0; i < 10; i++) { ch = getchar(); if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) { letter++; } else if (ch == ' ' || ch == '\n') { blank++; } else if (ch >= '0' && ch <= '9') { digit++; } else { other++; } } printf("letter = %d, blank = %d, digit = %d, other = %d\n", letter, blank, digit, other); return 0; } ``` ### 方法二:统计字符串中各类字符个数 此方法用于统计输入字符串中各类字符个数,包括大写字母、小写字母、空格数字其他字符。 ```c #include <stdio.h> #define MAXS 100 void StringCount(char *s) { int ALP = 0, alp = 0, blank = 0, number = 0, rest = 0; for (int i = 0; s[i] != '\0'; i++) { if (s[i] >= 'A' && s[i] <= 'Z') { ALP++; } else if (s[i] >= 'a' && s[i] <= 'z') { alp++; } else if (s[i] == ' ') { blank++; } else if (s[i] >= '0' && s[i] <= '9') { number++; } else { rest++; } } printf("%d %d %d %d %d\n", ALP, alp, blank, number, rest); } int main() { char s[MAXS]; gets(s); StringCount(s); return 0; } ``` ### 方法三:统计多行字符串中各类字符个数 此方法用于统计多行字符串中各类字符个数。 ```c #include <stdio.h> int main() { char s[3][80]; int i, j; int arr[3][5] = {0}; for (i = 0; i < 3; i++) { gets(s[i]); } for (i = 0; i < 3; i++) { for (j = 0; s[i][j] != '\0'; j++) { if (s[i][j] >= 'A' && s[i][j] <= 'Z') { arr[i][0]++; } if (s[i][j] >= 'a' && s[i][j] <= 'z') { arr[i][1]++; } if (s[i][j] >= '0' && s[i][j] <= '9') { arr[i][2]++; } if (s[i][j] == ' ') { arr[i][3]++; } else { arr[i][4]++; } } } for (i = 0; i < 3; i++) { for (j = 0; j < 4; j++) { printf("%-5d", arr[i][j]); } printf("\n"); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值