SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Initials Printing Program in Java and Python

19 October 2012

Initials printing program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program to accept a sentence and print only the first letter of each word of the sentence in capital letters separated by a full stop.

INPUT: Enter any sentence: This is a cat OUTPUT: T.I.A.C. INPUT: Enter any sentence: Java for school students OUTPUT: J.F.S.S.

Algorithm:

Step 1: Start.

Step 2: Accept a sentence from the user.

Step 3: Add one blank space at the beginning of the sentence so the first word can be handled like every other word.

Step 4: Convert the complete sentence to uppercase.

Step 5: Find and store the length of the modified sentence.

Step 6: Run a loop from index 0 to length - 1.

Step 7: Extract the current character using charAt(i).

Step 8: If the current character is a blank space, print the next character charAt(i + 1) followed by a full stop.

Step 9: Continue scanning until the end of the sentence is reached.

Step 10: Stop.

Explanation:

The program uses the fact that the first letter of every word comes immediately after a blank space. To make this logic work for the first word also, one blank space is added at the beginning of the input sentence.

The sentence is converted to uppercase before scanning, so all initials are printed as capital letters even if the user enters lowercase or mixed-case words.

The loop reads the sentence character by character using charAt(i). Whenever the current character is a blank space, the next character charAt(i + 1) is the first letter of a word.

The program prints that next character followed by a full stop. This produces output such as J.F.S.S. without needing to store the individual words separately.

The logic depends on identifying word boundaries rather than simply printing fixed positions. A name or sentence may contain words of different lengths, so the program must detect where each word begins. Usually the first character is printed directly, and then any character that follows a blank space becomes the initial of the next word. Converting to uppercase before processing ensures the initials appear in a uniform form. This makes the solution a useful exercise in scanning strings character by character and understanding how spaces separate words.

Java Program:

Java
/**
* The class Initials takes a sentence/name as input and prints its initials.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class Initials
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter any sentence: ");
        String s = sc.nextLine();

        // Add a blank space before the sentence so the first word is also detected.
        s = " " + s;

        // Convert to uppercase so all initials are printed as capital letters.
        s = s.toUpperCase();

        int l = s.length();

        System.out.print("Output = ");

        for(int i = 0; i < l - 1; i++)
        {
            char x = s.charAt(i);

            // The first letter of a word is found immediately after a blank space.
            if(x == ' ')
            {
                System.out.print(s.charAt(i + 1) + ".");
            }
        }
    }
}

Equivalent Python Program:

Python
s = input("Enter any sentence: ")

# Convert to uppercase so every initial is printed as a capital letter.
s = s.upper()
words = s.split()

# Print initials on the same output line.
print("Output: ", end="")

# Each word contributes its first character to the initials.
for i in range(len(words)):
    print(words[i][0] + ".", end="")

Output:

Enter any sentence: Java for school students Output: J.F.S.S.

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →