首页 > 试题广场 >

统计大写字母个数

[编程题]统计大写字母个数
  • 热度指数:137709 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
找出给定字符串中大写字符(即'A'-'Z')的个数。
数据范围:字符串长度:
字符串中可能包含空格或其他字符
进阶:时间复杂度:,空间复杂度:

输入描述:

对于每组样例,输入一行,代表待统计的字符串



输出描述:

输出一个整数,代表字符串中大写字母的个数

示例1

输入

A 1 0 1 1150175017(&^%&$vabovbaoadd 123#$%#%#O

输出

2
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());

    }
}

1、通过 replaceAll 方法替换非大写字母为空字符串
2、输出替换后的字符串长度即可
发表于 2023-08-29 18:41:21 回复(0)
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);
    }
}

发表于 2023-08-13 15:52:20 回复(0)
import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        List<Characterupper = new ArrayList<>();
        upper.add('A');
        upper.add('B');
        upper.add('C');
        upper.add('D');
        upper.add('E');
        upper.add('F');
        upper.add('G');
        upper.add('H');
        upper.add('I');
        upper.add('J');
        upper.add('K');
        upper.add('L');
        upper.add('M');
        upper.add('N');
        upper.add('O');
        upper.add('P');
        upper.add('Q');
        upper.add('R');
        upper.add('S');
        upper.add('T');
        upper.add('U');
        upper.add('V');
        upper.add('W');
        upper.add('X');
        upper.add('Y');
        upper.add('Z');

        int sum = 0 ;
        if (!"".equals(s.trim())) {
            char[] c = s.toCharArray();
            int len = c.length;
            for (int i = 0; i < len; i++) {
                if(upper.contains(c[i])){
                    sum = sum + 1;
                }
            }
        }
        System.out.print(sum);
    }
}
发表于 2022-10-31 21:43:31 回复(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 l = str.replaceAll("[A-Z]","").length();
            System.out.print(str.length()-l);
        }
    }
}

发表于 2022-10-03 15:42:08 回复(0)

import java.util.*;
import javax.script.*;
public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String s=scan.nextLine();
        String str=s.replaceAll("[A-Z]{0,}","");
        
       System.out.println(s.length()-str.length());
    }

    
}

发表于 2022-07-04 15:30:17 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String nextLine = sc.nextLine();
        int finalNum = 0;
        for (int i = 0; i < nextLine.length(); i++) {
            char charAt = nextLine.charAt(i);
            if (charAt >= 'A' && charAt <= 'Z') {
                ++finalNum;
            }
        }
        System.out.println(finalNum);
    }
}
发表于 2022-06-15 15:22:05 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[]args){
        Scanner in=new Scanner(System.in);
        while(in.hasNext()){
            String s=in.nextLine();
            String newS=s.replaceAll("[A-Z]+","");
            System.out.println(s.length()-newS.length());
        }
        in.close();
    }
}
发表于 2022-06-01 13:09:22 回复(0)
import java.util.Scanner;
public class Main {
    public static void main (String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        System.out.println(input.length() - input.replaceAll("[A-Z]", "").length());
    }
}
发表于 2022-05-28 22:39:20 回复(0)
正则表达式 统计

import java.util.*;
public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str1 = scanner.nextLine();
            String regex = "[A-Z]";
            str1 = str1.replaceAll(regex, "*");
            int count=0;
            for (char c: str1.toCharArray()) {
                if (String.valueOf(c).equals("*")) {
                    count++;
                }
            }
            System.out.print(count);
           
        }
    }
}




发表于 2022-05-10 17:06:17 回复(1)
比较简单。
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);
        }
    }
}

发表于 2022-04-25 23:38:35 回复(0)
正则表达式去掉所有大写字母,长度减一下,简单快乐
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());
    }
}

发表于 2022-04-22 09:48:05 回复(0)
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);
        }
    }
}

发表于 2022-04-17 23:17:46 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] agrs) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        int len = str.length();
        // 用空字符串替换所有大写字母并获取长度
        int len2 = str.replaceAll("[A-Z]", "").length();
        System.out.println((len - len2));
    }
}
发表于 2022-04-13 19:57:54 回复(0)


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String ch = sc.nextLine();
            int nums = 0;
            for (int i = 0; i < ch.length(); i++) {
                if (ch.charAt(i) >= 'A' && ch.charAt(i)  <= 'Z') {
                    nums++;
                }
            }
            System.out.println(nums);
        }
    }

}
发表于 2022-03-27 22:05:35 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s = sc.nextLine();
            String s1 = s.replaceAll("[^A-Z]", "");
            System.out.println(s1.length());
        }
    }

}

发表于 2022-03-27 20:30:43 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String ss = s.replaceAll("[^A-Z]+", "");
        System.out.println(ss.length());
    }
}
发表于 2022-03-15 21:30:17 回复(0)
import java.util.Scanner;

/**
 * 描述
 * 找出给定字符串中大写字符(即'A'-'Z')的个数。
 * 数据范围:字符串长度:1≤∣s∣≤250
 * 字符串中可能包含空格或其他字符
 * 输入描述:
 * 本题含有多组样例输入
 * 对于每组样例,输入一行,代表待统计的字符串
 * 输出描述:
 * 对于每组样例,输出一个整数,代表字符串中大写字母的个数
 */

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(!Character.isUpperCase(c)){   //使用Character的方法比对
                    continue;
                }else
                    count++;
            }
            System.out.println(count);
        }
    }
}

发表于 2022-02-10 15:39:55 回复(0)
这应该是最简单的一题了
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);
        }
    }
}


发表于 2021-12-24 15:29:16 回复(0)

问题信息

难度:
43条回答 37967浏览

热门推荐

通过挑战的用户

查看代码