题解 | #判断各类型字符个数#

判断各类型字符个数

http://www.nowcoder.com/practice/4ccc155e474e4d4c83cfde116dcf2218

方法一:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int numbers = 0;
        int words = 0;
        int space = 0;
        int other = 0;
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();

        for(int i=str.length()-1; i>=0; i--){
            char c = str.charAt(i);
            if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){
                words++;
            }else if (c >= '0' && c <= '9'){
                numbers++;
            }else if (c == ' '){
                space++;
            }else{
                other++;
            }                
        }
            

        System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other);
    }
}

方法二,API工程师:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int numbers = 0;
        int words = 0;
        int space = 0;
        int other = 0;
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();

        for(int i=str.length()-1; i>=0; i--){
            char c = str.charAt(i);
            if(Character.isLetter(c)){
                words++;
            }else if (Character.isDigit(c)){
                numbers++;
            }else if (Character.isWhitespace(c)){
                space++;
            }else{
                other++;
            }                
        }
            

        System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other);
    }
}
全部评论

相关推荐

牛客40297450...:不是研究生强,是你强
点赞 评论 收藏
分享
牛客54175811...:今年对双非很难。1、争取一段大厂实习经历,2、狂磕八股,3、再跑个难度提升的项目。
点赞 评论 收藏
分享
评论
26
2
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务