找出给定字符串中大写字符(即'A'-'Z')的个数。
数据范围:字符串长度:
字符串中可能包含空格或其他字符
进阶:时间复杂度:,空间复杂度:
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 a = in.nextLine(); a = a.replaceAll("[^A-Z]",""); System.out.print(a.length()); } }
import java.util.Scanner; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); // 计数器 int count = 0; for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); if (Character.isUpperCase(c)){ count++; } } System.out.println(count); } }
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 l = str.replaceAll("[A-Z]","").length(); System.out.print(str.length()-l); } } }
import java.util.Scanner; import java.util.WeakHashMap; /* HJ84 统计大写字母个数 简单 通过率:44.75% 知识点:字符串 描述 找出给定字符串中大写字符(即'A'-'Z')的个数。 数据范围:字符串长度:1≤∣s∣≤250 字符串中可能包含空格或其他字符 进阶:时间复杂度:O(n)\O(n) ,空间复杂度:O(n)\O(n) 输入描述:对于每组样例,输入一行,代表待统计的字符串 输出描述:输出一个整数,代表字符串中大写字母的个数 示例1 输入:A 1 0 1 1150175017(&^%&$vabovbaoadd 123#$%#%#O 输出:2 * */ public class Main { public static void main(String[] args) { methodMy(); } private static void methodMy() { Scanner in = new Scanner(System.in); while (in.hasNext()){ String str = in.nextLine(); char[] chars = str.toCharArray(); int count=0; for (char aChar : chars) { if (aChar >= 'A' && aChar <= 'Z') { count++; } } System.out.println(count); } } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String inp = sc.nextLine(); String inp_ = inp.replaceAll("[A-Z]+", ""); System.out.println(inp.length()-inp_.length()); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input; while ((input = br.readLine()) != null) { int before = input.length(); int after = input.replaceAll("[A-Z]", "").length(); System.out.println(before - after); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String str = sc.nextLine(); int count = 0; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c >= 'A' && c <= 'Z') { count++; } } System.out.println(count); } } }