首页 > 试题广场 >

统计字符

[编程题]统计字符
  • 热度指数:153639 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。

数据范围:输入的字符串长度满足


输入描述:

输入一行字符串,可以有空格



输出描述:

统计其中英文字符,空格字符,数字字符,其他字符的个数

示例1

输入

1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][

输出

26
3
10
12
记着ascII码就比较简单了。

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
String str = in.nextLine();

int letter = 0;
int space = 0;
int num = 0;
int other = 0;
for(int i=0;i<str.length();i++){
char c = str.charAt(i);
if(c>=65 && c<=90){
letter++;
}
else if(c>=97 && c<=122){
letter++;
}

else if(c == ' ') {
space++;
}

else if(c >=48 && c<=57){
num++;
}
else{
other++;
}
}

System.out.println(letter);
System.out.println(space);

System.out.println(num);

System.out.println(other);

}
}

编辑于 2024-04-24 16:45:08 回复(0)
无脑上:
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        int letter = 0;
        int blank = 0;
        int digit = 0;
        int other = 0;
        char[] chars = line.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (Character.isLetter(chars[i])) {
                letter++;
            } else if (Character.isDigit(chars[i])) {
                digit++;
            } else if (chars[i] == ' ') {
                blank++;
            } else {
                other++;
            }
        }
        System.out.println(letter);
        System.out.println(blank);
        System.out.println(digit);
        System.out.println(other);
    }


编辑于 2024-04-08 21:47:17 回复(0)
毫无技巧的暴力输出
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String val = in.nextLine();
        char[] cs = val.toCharArray();
        //字符
        int a = 0;
        //空格
        int b = 0;
        //数字
        int c = 0;
        //其他
        int d = 0;
        for (char v : cs) {
            if (v >= '0' && v <= '9') {
                c = c + 1;
            } else if (v == ' ') {
                b = b + 1;
            } else if ((v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') ) {
                a = a + 1;
            } else {
                d = d + 1;
            }
        }
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}


编辑于 2024-04-08 12:48:43 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String s = in.nextLine();
            int letter = 0;
            int blankSpace = 0;
            int num = 0;
            int other = 0;
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (Character.isWhitespace(c)) {
                    blankSpace++;
                } else if (Character.isLetter(c) ) {
                    letter++;
                } else if (Character.isDigit(c)) {
                    num++;
                } else {
                    other++;
                }
            }
            System.out.println(letter);
            System.out.println(blankSpace);
            System.out.println(num);
            System.out.println(other);
        }
    }
}

编辑于 2024-01-09 10:04:28 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
       char [] arr = in.nextLine().toCharArray();
       int [] a = new int[4];
       for(int i = 0; i < arr.length; i++){
            if(Character.isLetter(arr[i])){
                a[0]++;
            }else if(arr[i] == ' '){
                a[1]++;
            }else if(Character.isDigit(arr[i])){
                a[2]++;
            }else{
               a[3]++;
            }
       }
       System.out.println(a[0]+"\n"+a[1]+"\n"+a[2]+"\n"+a[3]);
    }
}

发表于 2023-11-24 13:13:59 回复(0)
public class HJ40 {

    public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            // 注意 hasNext 和 hasNextLine 的区别
            while (in.hasNextLine()){
                String str = in.nextLine();
                count(str);
//                methodReplace(str);
                bytecodeCount(str);
            }

    }

    private static void methodReplace(String str) {
        int length = str.length();
        String replaced = str.replaceAll("[\\u4E00-\\u9FA5A-Za-z]", "");
        System.out.println(length - replaced.length());
        length = replaced.length();
        replaced = replaced.replaceAll(" ", "");
        System.out.println(length - replaced.length());
        length = replaced.length();
        replaced = replaced.replaceAll("[0-9]", "");
        System.out.println(length - replaced.length());
        length = replaced.length();
        System.out.println(length);
    }

    private static void count(String str) {
        int a = 0, e = 0, n = 0, o = 0;
        for (int i = 0; i < str.length(); i++) {
            char charred = str.charAt(i);
            String item = Character.toString(charred);
            if (item.matches("[\\u4E00-\\u9FA5A-Za-z]")) {
                a++;
            }else if (item.matches("[0-9]")){
                n++;
            }else if (item.equals(" ")){
                e ++;
            }else {
                o ++;
            }
        }
        System.out.println(a + "\n" + e + "\n" + n + "\n" + o);
    }
    
    private static void bytecodeCount(String str) {
        int a = 0, e = 0, n = 0, o = 0;
        for (int i = 0; i < str.length(); i++) {
            char item = str.charAt(i);
            //利用 字码表
            if((item >= 0x4e00)&&(item <= 0x9fbb) || (item >= 65 && item <= 90) || (item >= 97 && item <= 122)) {
                a++;
            }else if (item >= 48 && item <= 57){
                n++;
            }else if (Character.isSpaceChar(item)){
                e++;
            }else {
                o ++;
            }
        }
        System.out.println(a + "\n" + e + "\n" + n + "\n" + o);
    }
}

发表于 2023-09-17 00:32:39 回复(0)
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = br.readLine()) != null) {
            // 存储不同字符的次数,可以用双值型数据类型hashMap
            HashMap<String, Integer> map = new HashMap<>();

            for (int i = 0; i < line.length(); i++) {
                char c = line.charAt(i);
                if (Character.isLetter(c)) {
                    String key = "character";
                    map.put(key, map.getOrDefault(key, 0) + 1);
                } else if (c == ' ') {
                    String key = "space";
                    map.put(key, map.getOrDefault(key, 0) + 1);
                } else if (c >= '0' && c <= '9') {
                    String key = "digital";
                    map.put(key, map.getOrDefault(key, 0) + 1);
                } else {
                    String key = "other";
                    map.put(key, map.getOrDefault(key, 0) + 1);
                }
            }

            System.out.println(map.getOrDefault("character", 0));
            System.out.println(map.getOrDefault("space", 0));
            System.out.println(map.getOrDefault("digital", 0));
            System.out.println(map.getOrDefault("other", 0));
        }
    }
}

发表于 2023-08-09 11:00:40 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String input = in.nextLine();
            funHJ40(input);
        }
    }
    public static void funHJ40(String str) {
        System.out.println(str.length() - str.replaceAll("[A-Za-z]", "").length());
        System.out.println(str.length() - str.replaceAll("\\s", "").length());
        System.out.println(str.length() - str.replaceAll("[0-9]", "").length());
        System.out.println(str.length() - str.replaceAll("[^0-9a-zA-Z\\s]",
                           "").length());
    }
}

发表于 2023-06-17 21:29:57 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        int eNum = 0;
        int sNum = 0;
        int mNum = 0;
        int oNum = 0;

        char[] chars = str.toCharArray();
        for (char aChar : chars) {
            if((aChar >= 65 && aChar <= 90) || (aChar >= 97 && aChar <= 122)) {
                eNum++;
            }else if(aChar >= 48 && aChar <= 57) {
                mNum++;
            }else if(aChar == 32) {
                sNum++;
            }else oNum++;
        }
        System.out.println(eNum);
        System.out.println(sNum);
        System.out.println(mNum);
        System.out.println(oNum);
    }
}


发表于 2023-06-06 23:11:13 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a = in.nextLine();
        int num = 0;
        int letter = 0;
        int kou = 0;
        int other = 0;
        for (int i = 0; i < a.length(); i++) {
            char cha = a.charAt(i);
            if (Character.isDigit(cha)) {
                num++;
                continue;
            }
            if (Character.isLetter(cha)) {
                letter++;
                continue;
            }
            if (Character.isSpaceChar(cha)) {
                kou++;
                continue;
            }
            other++;
        }
        System.out.println(letter);
        System.out.println(kou);
        System.out.println(num);
        System.out.println(other);
    }
}

发表于 2023-05-30 16:44:54 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str=in.nextLine();
        int c=0;
        int space=0;
        int math=0;
        int other=0;
        for(int i=0;i<str.length();i++){
            if(Character.isLetter(str.charAt(i))){
                c++;
            }else if(Character.isDigit(str.charAt(i))){
                math++;
            }else if(Character.isSpace(str.charAt(i))){
                space++;
            }else{
                other++;
            }

        }
        System.out.println(c);
        System.out.println(space);
        System.out.println(math);
        System.out.println(other);
    }
}

发表于 2023-05-28 15:18:27 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();

        int total_lenth = str.length();

        str = str.replaceAll("[a-zA-Z]", "");
        System.out.println(total_lenth-str.length());//英文
        String str1 = str.replaceAll("\\s", "");
        System.out.println(str.length()-str1.length());//空格
        String str2 = str1.replaceAll("[0-9]", "");
        System.out.println(str1.length()-str2.length());//数字
        System.out.println(str2.length());//其他
    }
}
发表于 2023-03-29 17:09:08 回复(0)
\
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str=in.nextLine();
            int a=str.length()-str.replaceAll("[a-zA-Z]","").length();
            int b=str.length()-str.replaceAll(" ","").length();
            int c=str.length()-str.replaceAll("[0-9]","").length();
            int d=str.length()-a-b-c;
            System.out.println(a);
            System.out.println(b);
            System.out.println(c);
            System.out.println(d);
        }
    }
}

发表于 2023-03-05 16:31:35 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int i = 0, j = 0, m = 0, n = 0;
        for (char c : str.toCharArray()) {
            if (Character.isLetter(c)) {
                i++;
            } else if (' ' == c) {
                j++;
            } else if (c >= '0' && c <= '9') {
                m++;
            } else {
                n++;
            }
        }
        System.out.println(i);
        System.out.println(j);
        System.out.println(m);
        System.out.println(n);
    }
}

发表于 2023-02-22 08:42:35 回复(0)
用 match 做
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String s=in.nextLine();
        int count1=0;
        int count2=0;
        int count3=0;
        int count4=0;
        for(int i=0; i<s.length();i++){
            String temp=s.charAt(i)+"";
            if(temp.matches("[a-zA-Z]")){
                count1++;
            }
            else if(temp.matches(" ")){count2++;}
            else if(temp.matches("[0-9]")){count3++;}
            else{count4++;}
        }
        System.out.println(count1);
        System.out.println(count2);
        System.out.println(count3);
        System.out.println(count4);
    }
}

发表于 2023-02-17 21:19:51 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int[] count = new int[4];
        for(int i = 0;i < line.length();i++){
            char ch = line.charAt(i);
            if(Character.isLetter(ch)){
                count[0]++;
            }else if(ch == ' '){
                count[1]++;
            }else if(Character.isDigit(ch)){
                count[2]++;
            }else{
                count[3]++;
            }
        }
        for(int i = 0;i < 4;i++){
            System.out.println(count[i]);
        }
    }
}

发表于 2022-08-12 15:47:58 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            //获取字符串
            String str = scanner.nextLine();
            char[] chs=str.toCharArray();
            int letterNum=0;
            int blankNum=0;
            int digitalNum=0;
            int othersNum=0;
            for (int i = 0; i < str.length(); i++) {
                if (Character.isLetter(chs[i])) {
                    letterNum++;
                    continue;
                }
                if (Character.isDigit(chs[i])) {
                    digitalNum++;
                    continue;
                }
                if (Character.isWhitespace(chs[i])) {
                    blankNum++;
                    continue;
                }
                othersNum++;
            }
            System.out.println(letterNum);
            System.out.println(blankNum);
            System.out.println(digitalNum);
            System.out.println(othersNum);
        }
        scanner.close();
    }
}

发表于 2022-08-11 19:22:21 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception {
        /*BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = null;*/
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            char[] toSum = scanner.nextLine().toCharArray();
            int isEnglishChar = 0;
            int isSpaceChar = 0;
            int isDigitChar = 0;
            int isSpecialChar = 0;
            for(int i = 0;i < toSum.length;i++){
                if(Character.isLetter(toSum[i])){
                    isEnglishChar++;
                }else if(toSum[i] == ' '){
                    isSpaceChar++;
                }else if(Character.isDigit(toSum[i])){
                    isDigitChar++;
                }else{
                    isSpecialChar++;
                }
            }
            System.out.println(isEnglishChar);
            System.out.println(isSpaceChar);
            System.out.println(isDigitChar);
            System.out.println(isSpecialChar);
        }
    }
}

发表于 2022-07-29 16:14:37 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        int a = 0, b = 0, c =0, d = 0;
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (Character.isLetter(ch)) {
                a++;
            } else if (ch == ' ') {
                b++;
            } else if (Character.isDigit(ch)) {
                c++;
            } else {
                d++;
            }
        }

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}

发表于 2022-07-07 14:22:35 回复(0)

问题信息

难度:
73条回答 45201浏览

热门推荐

通过挑战的用户

查看代码