Home / Programs / Write a program to input a sentence and convert it into uppercase and count and display the total number of words starting with a letter 'A'.
Programming Example

Write a program to input a sentence and convert it into uppercase and count and display the total number of words starting with a letter 'A'.

👁 162 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Write a program to input a sentence and convert it into uppercase and count and display the total number of words starting with a letter 'A'.

Example:

Sample Input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING.

Sample Output: Total number of words starting with letter 'A' = 4

Program Code

import java.util.Scanner;

public class WordsWithLetterA
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a string: ");
        String str = in.nextLine();
        str = " " + str; //Add space in the begining of str
        int c = 0;
        int len = str.length();
        str = str.toUpperCase();
        for (int i = 0; i < len - 1; i++) {
            if (str.charAt(i) == ' ' && str.charAt(i + 1) == 'A')
                c++;
        }
        System.out.println("Total number of words starting with letter 'A' = " + c);
    }
}

Output

Enter a string:
A tech number has even number of digits.
Total number of words starting with letter 'A' = 1
Press any key to continue . . .



How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.