JAVA45. 判断各类型字符个数
描述
输入一行字符串,分别统计出其中英文字母、空格、数字和其它字符的个数输入描述
控制台随机输入一串字符串输出描述
输出字符串中包含的英文字母个数,数字个数,空格个数,其它字符个数(格式为:英文字母x数字x空格x其他x),预设代码中已给出输出示例1
输入:
!@#¥% asdyuihj 345678
输出:
英文字母8数字6空格2其他5
Java 解法, 执行用时: 23ms, 内存消耗: 10764KB, 提交时间: 2022-02-08
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(); //write your code here...... for(int i=0 ; i<str.length();i++){ char c = str.charAt(i); if(c>='0'&& c<='9'){ numbers++; }else if(c>='a' && c<='z' || c>='A' && c<='Z'){ words++; }else if(c==' '){ space++; }else{ other++; } } System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other); } }
Java 解法, 执行用时: 24ms, 内存消耗: 10528KB, 提交时间: 2022-02-09
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(); //write your code here...... for(int i=0;i<str.length();i++){ char ch=str.charAt(i); if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'){ words++; }else if(ch>='0'&&ch<='9'){ numbers++; }else if(ch==' '){ space++; }else{ other++; } } System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other); } }
Java 解法, 执行用时: 26ms, 内存消耗: 10436KB, 提交时间: 2021-10-30
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(); //write your code here...... for(int i=0; i<str.length(); i++){ if(Character.isDigit(str.charAt(i))){ numbers++; }else if(Character.isAlphabetic(str.charAt(i))){ words++; }else if(Character.isSpace(str.charAt(i))){ space++; }else{ other++; } } System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other); } }
Java 解法, 执行用时: 26ms, 内存消耗: 10500KB, 提交时间: 2022-02-08
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(); //write your code here...... for(int i=0;i<str.length();i++){ char c = str.charAt(i); if(c>='a'&&c<='z'){ words++; }else if(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); } }
Java 解法, 执行用时: 26ms, 内存消耗: 10576KB, 提交时间: 2021-11-19
import java.util.Scanner; import java.util.regex.*; 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(); // System.out.println(symbolNumber.string(s,"[a-z|A-Z]")); //字母个数 // System.out.println(symbolNumber.string(s,"[0-9]")); //数字个数 String r1="[a-z|A-Z]";//字母匹配 String r2="[0-9]";//数字匹配 String r3="\\s"; Pattern p1=Pattern.compile(r1); Pattern p2=Pattern.compile(r2); Pattern p3=Pattern.compile(r3); Matcher m1=p1.matcher(str); Matcher m2=p2.matcher(str); Matcher m3=p3.matcher(str); while (m1.find()) { words++; } while (m2.find()) { numbers++; } while (m3.find()) { space++; } other=str.length()-words-numbers-space; //write your code here...... System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other); } }