Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Initials with Surname

Updated 21 Oct 2026

This program reads a person's full name and prints it in short form: the first letter of every word except the last, each followed by a full stop, and then the last word (the surname) in full. Mohandas Karamchand Gandhi becomes M. K. Gandhi. It uses trim(), lastIndexOf(), substring() and charAt() only, with no split() or StringTokenizer, and everything is written in the main method.

Definition

The words of a name are separated by one or more spaces. The surname is the last word; every word before it contributes only its first letter, written as a capital and followed by ..

  • Mohandas Karamchand Gandhi gives M. K. Gandhi.
  • Mahatma Gandhi gives M. Gandhi.
  • Avul Pakir Jainulabdeen Abdul Kalam (extra spaces, with leading and trailing spaces as well) gives A. P. J. A. Kalam.

Rules this program follows:

  • Single word: a name of one word is printed as it is, exactly as typed (Kalam gives Kalam, kalam gives kalam).
  • Case: initials are always capitals, but the surname is printed in the case typed. sachin ramesh tendulkar gives S. R. tendulkar, not S. R. Tendulkar.
  • Capital conversion: the program uses Character.toUpperCase(ch) to turn the initial into a capital.
  • Blank name: an empty line or a line of only spaces prints INVALID INPUT.
  • Only spaces separate words, and punctuation is not special (Dr. would give D.).

Algorithm

  1. Read the name into name and remove the spaces at its two ends with trim(). If it is empty, print INVALID INPUT and stop.
  2. Find the surname: p = name.lastIndexOf(' ') is the position of the last space (-1 if there is none), and sur = name.substring(p + 1) is everything after it. If the name has no space, the whole name is the surname.
  3. Let end = name.length() - sur.length(), the number of characters before the surname.
  4. Set ini = "". For i from 0 to end - 1, a word starts at a character that is not a space and is either the first character or follows a space. For such a character, add its capital form followed by ". " to ini.
  5. Print ini followed by sur.

Program

The for loop in main looks only at the part of the name before the surname and picks the first letter of each word.

import java.util.Scanner;

/* Program Initials reads a full name and prints the first letter of all the
   words except the last, and the last word (surname) in full.
   Example: Mohandas Karamchand Gandhi  ->  M. K. Gandhi */
public class Initials
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the full name: ");
        String name = sc.nextLine().trim();      // the full name, without leading or trailing spaces
        if (name.length() == 0)
            System.out.println("INVALID INPUT");
        else
        {
            int p = name.lastIndexOf(' ');       // position of the last space, -1 if none
            String sur = name.substring(p + 1);  // the surname; p = -1 gives the whole name
            String ini = "";                     // the initials, each followed by ". "
            int end = name.length() - sur.length();   // characters before the surname
            for (int i = 0; i < end; i++)
            {
                char ch = name.charAt(i);
                // a word starts at a non-space character that is the first character or follows a space
                if (ch != ' ' && (i == 0 || name.charAt(i - 1) == ' '))
                    ini = ini + Character.toUpperCase(ch) + ". ";
            }
            System.out.println(ini + sur);
        }
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the input typed by the user
nameStringThe full name typed by the user, without leading or trailing spaces
pintPosition of the last space in name, -1 if there is none
surStringThe last word of the name, the surname
iniStringThe initials, each followed by a full stop and a space
endintNumber of characters before the surname
iintLoop counter over the characters before the surname
chcharThe character being examined

Sample input and output

Sample 1

Enter the full name: Mohandas Karamchand Gandhi
M. K. Gandhi

Sample 2

Enter the full name: sachin ramesh tendulkar
S. R. tendulkar

Sample 3

Enter the full name:   Avul   Pakir  Jainulabdeen   Abdul    Kalam  
A. P. J. A. Kalam

Sample 4

Enter the full name: Kalam
Kalam

Sample 5

Enter the full name: Mahatma Gandhi
M. Gandhi

One value per line

Dry run

Input Mohandas Karamchand Gandhi (26 characters: Mohandas at 0 to 7, space at 8, Karamchand at 9 to 18, space at 19, Gandhi at 20 to 25). trim() changes nothing. p = 19 (the position of the last space) and sur = Gandhi (length 6), so end = 26 - 6 = 20.

ichNon-space and (i = 0 or space before)?ini after this step
0Myes, first characterM.
1 to 7o, h, a, n, d, a, snoM.
8spacenoM.
9Kyes, space beforeM. K.
10 to 18a, r, a, m, c, h, a, n, dnoM. K.
19spacenoM. K.

The loop stops at i = 20, the start of the surname. The program prints ini + sur = M. K. Gandhi, the same as the real run above.

Variations

  1. Surname first "Write a program to accept a full name and display it as the surname followed by the initials, e.g. Gandhi M. K."
  2. Initials only "Write a program to accept a full name and display only the initials in capital letters, e.g. MKG."
  3. Capitalise words "Write a program to accept a sentence and convert the first letter of every word to capital."