在一行上输入一个长度为
的字符串。
第一行输出一个整数,代表字符串中英文字母的个数。
第二行输出一个整数,代表字符串中空格的个数。
第三行输出一个整数,代表字符串中数字的个数。
第四行输出一个整数,代表字符串中其它字符的个数。
1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][
26 3 10 12
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();
System.out.println(str.replaceAll("[^a-zA-Z]","").length());
System.out.println(str.replaceAll("[^ ]","").length());
System.out.println(str.replaceAll("[^0-9]","").length());
System.out.println(str.replaceAll("[a-zA-Z0-9 ]","").length());
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
int n = s.length();
int n1 = 0, n2 = 0, n3 = 0, n4 = 0;
// 方法1
// n1 = n - s.replaceAll("[a-zA-Z]", "").length();
// n2 = n - s.replaceAll(" ", "").length();
// n3 = n - s.replaceAll("[0-9]", "").length();
// n4 = n - n1 - n2 - n3;
// 方法2
for (char c : s.toCharArray()) {
if (Character.isLetter(c)) n1++;
else if (Character.isSpaceChar(c)) n2++;
else if (Character.isDigit(c)) n3++;
else n4++;
}
System.out.printf("%d\n%d\n%d\n%d", n1, n2, n3, n4);
}
}
package com.shisui.easy;
import java.util.Scanner;
public class count_characters {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String string = sc.nextLine();
int letterCount = 0;
int spaceCount = 0;
int numberCount = 0;
int otherCount = 0;
for (int i = 0; i < string.length(); i++) {
char ch = string.charAt(i);
if(Character.isLetter(ch)){
letterCount++;
}else if(Character.isSpaceChar(ch)){
spaceCount++;
}else if(Character.isDigit(ch)){
numberCount++;
}else {
otherCount++;
}
}
System.out.println(letterCount);
System.out.println(spaceCount);
System.out.println(numberCount);
System.out.println(otherCount);
}
}
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.hasNext()) { // 注意 while 处理多个 case
String str = in.nextLine();
int englishNum = 0, spaceNum = 0, digitNum = 0, otherNum = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
englishNum++;
} else if (c >= '0' && c <= '9') {
digitNum++;
} else if (c == ' ') {
spaceNum++;
} else {
otherNum++;
}
}
System.out.println(englishNum);
System.out.println(spaceNum);
System.out.println(digitNum);
System.out.println(otherNum);
}
}
} 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);
} 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);
}
} 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);
}
}
} 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]);
}
} 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);
}
} 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));
}
}
}