7-43 sdut-循环-6-统计字符

7-43 sdut-循环-6-统计字符

首先给出所输入字符的个数N,然后给出N个字符,统计其中英文字母、空格、数字字符和其他字符的个数。
输入格式:

第一行为字符的个数N;
第二行为N个字符。

输出格式:

在四行内输出:
letters=英文字母个数
blanks=空格个数
digits=数字字符个数

输入样例:

10
AaZ & 15Az

输出样例:

letters=5
blanks=2
digits=2
others=1
import java.util.Scanner;
public class Main{
 public static void main(String[] args){
    
     Scanner cin=new Scanner(System.in);
     int n=cin.nextInt();
     cin.nextLine();
     String str=cin.nextLine();
     while(str.length()<n)
     {
         String str1=cin.nextLine();
         str=str+'\n'+str1;
     }
     int letters=0;
     int blanks=0;
     int digits=0,others=0;
     for(int i=0;i<n;i++)
     {
      int x=str.charAt(i);
         if(x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z')
         letters++;
         else if(x>='0'&&x<'9')
             digits++;
         else if(x==' ')
             blanks++;
         else
             others++;
     }
     System.out.println("letters="+letters);
     System.out.println("blanks="+blanks);
     System.out.println("digits="+digits);
     System.out.print("others="+others);
 }  
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hzhhn.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值