Counting Uppercase and Lowercase Letters in a String.

If you’re just starting out with Java programming, you might be looking for some fun and easy projects to practice your skills. In this guide, we’ll create a simple Java program that counts how many letters in a string are uppercase (like A, B, C) and how many are lowercase (like a, b, c).

We’ll break it down step by step so it’s easy to follow, even if you’re completely new to programming.

What Does This Program Do?

Let’s say you type in the text "Hello World!". This program will look at each letter in the text and count how many are uppercase (like H and W) and how many are lowercase (like e, l, o, etc.). At the end, it will tell you the numbers.

The Code

Here’s the simple Java code that does this:

import java.util.Scanner;

public class CountUppercaseAndLowercase {

    public static void main(String[] args) {
        // Create a Scanner object to read input from the user
        Scanner scan = new Scanner(System.in);

        // Ask the user to enter some text
        System.out.println("Enter some text:");
        String str = scan.nextLine();

        // These variables will keep track of the counts
        int upperCase = 0;  // To count uppercase letters
        int lowerCase = 0;  // To count lowercase letters

        // Go through each letter in the text
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);  // Get the letter at position i

            // If the letter is uppercase, increase the uppercase count
            if (Character.isUpperCase(ch)) {
                upperCase += 1;
            }

            // If the letter is lowercase, increase the lowercase count
            if (Character.isLowerCase(ch)) {
                lowerCase += 1;
            }
        }

        // Show the results
        System.out.println("Number of uppercase letters: " + upperCase);
        System.out.println("Number of lowercase letters: " + lowerCase);
    }
}

Let’s Break It Down

1. Getting Input from the User

First, the program asks you to type in some text:

Scanner scan = new Scanner(System.in);  // This lets us read what the user types
System.out.println("Enter some text:");  // Tells the user to enter text
String str = scan.nextLine();  // Stores the text in a variable called str
  • The Scanner is a tool that lets the program read what you type.
  • The str variable will hold the text you enter so that the program can work with it.

2. Setting Up Counters

Next, we set up two counters to keep track of how many uppercase and lowercase letters we find:

int upperCase = 0;  // Counts uppercase letters
int lowerCase = 0;  // Counts lowercase letters
  • upperCase starts at 0 and will increase each time we find an uppercase letter.
  • lowerCase starts at 0 and will increase each time we find a lowercase letter.

3. Looking at Each Letter

The program then goes through the text you entered, one letter at a time:

for (int i = 0; i < str.length(); i++) {
    char ch = str.charAt(i);  // Get the letter at position i
  • str.length() tells us how many letters are in the text.
  • str.charAt(i) gives us the letter at position i (starting from 0).

4. Counting Uppercase and Lowercase Letters

For each letter, the program checks if it’s uppercase or lowercase:

if (Character.isUpperCase(ch)) {
    upperCase += 1;  // Adds 1 to the uppercase count if the letter is uppercase
}

if (Character.isLowerCase(ch)) {
    lowerCase += 1;  // Adds 1 to the lowercase count if the letter is lowercase
}
  • Character.isUpperCase(ch) checks if the letter is uppercase. If it is, we add 1 to upperCase.
  • Character.isLowerCase(ch) checks if the letter is lowercase. If it is, we add 1 to lowerCase.

5. Showing the Results

Finally, the program tells you how many uppercase and lowercase letters it found:

System.out.println("Number of uppercase letters: " + upperCase);
System.out.println("Number of lowercase letters: " + lowerCase);

These lines show you the final counts.

Example Run

When you run the program and type in "Hello World!", it will output:

Number of uppercase letters: 2
Number of lowercase letters: 8

This tells you that there are 2 uppercase letters (H and W) and 8 lowercase letters in the text.

Why Is This Useful?

Learning to count uppercase and lowercase letters is a great way to practice working with strings (text) and loops (repeating tasks) in Java. This program also helps you get familiar with Java’s built-in tools, like the Scanner for reading user input and the Character class for checking if a letter is uppercase or lowercase.

Summary

  • Input: The program asks you to enter some text.
  • Counting: It then counts how many letters are uppercase and how many are lowercase.
  • Output: Finally, it tells you the counts.

This is a simple yet powerful way to start understanding how Java works with text. Try running the program with different inputs and see how it counts the letters!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top