Print All Capital Letters of a Given String in Java



The Character class is a subclass of the Object class, and it wraps a value of the primitive type char in an object. An object of type Character class contains a single field whose type is char. We can print all the capital letters of a given string using the following approaches -

Using isUpperCase() Method

The isUpperCase() is the method of the Character class. It accepts a character as a parameter and returns true if it is an uppercase letter and false if not.

To print all the uppercase letters in a string, we need to iterate each character using a for loop, and check whether the individual characters are uppercase letters or not, using the isUpperCase() method.

Syntax

Following is the syntax of the isUpperCase() method -

isUpperCase(char ch)

Where, ch A character that needs to be checked.

Example

The following program uses the isUpperCase() method of the Character class to identify all the capital letters in the given string "Welcome To Tutorials Point India" and print them one by one as a result -

public class PrintUpperCaseLetterStringTest {
   public static void main(String[] args) {
      String str = "Welcome To Tutorials Point India";
      System.out.println("All capital letters are: ");
      for(int i = 0; i < str.length(); i++) {
         if(Character.isUpperCase(str.charAt(i))) {
             System.out.println(str.charAt(i));
         }
      }
   }
}

The above program prints all the capital letters found in the given string -

All capital letters are: 
W
T
T
P

Using ASCII Comparison

This is another way to print all the capital letters from a given String using the ASCII comparison. An ASCII value stands for "American Standard Code for Information Interchange", which represents characters as numerical values.

The ASCII values of all the capital letters will be in the range of 65 to 90. To determine whether the specific character is an uppercase, you need to verify whether the ASCII value of it lies in the desired range.

Example

The below example will use the ASCII value comparison technique to print all the capital letters of the given string "" -

public class Main {
   public static void main(String[] args) {
      String str = "Hello From Tutorials Point";
      System.out.println("All capital letters are: ");
      for (char ch : str.toCharArray()) {
         if (ch >= 'A' && ch <= 'Z') {
            System.out.print(ch + " ");
         }
      }
   }
}

Following is the output of the above program -

All capital letters are: 
H F T P 
Updated on: 2025-05-14T11:57:53+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements