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

ISC Practicals and Theory - Initials with Surname (OOP)

Updated 21 Oct 2026

This program prints a full name in short form: the initials of all words except the last, each followed by a full stop, and then the last word (surname) in full, so Mohandas Karamchand Gandhi becomes M. K. Gandhi. In this version the name is passed to a parameterised constructor, which trims it; main reads the line, creates the object and calls display().

Definition

Words are separated by one or more spaces. The last word is the surname; every earlier word gives its first letter as a capital followed by ..

  • Mahatma Gandhi gives M. Gandhi.
  • Avul Pakir Jainulabdeen Abdul Kalam gives A. P. J. A. Kalam.

Rules followed:

  • A single-word name is printed unchanged, as typed (kalam gives kalam).
  • The surname keeps the case typed: sachin ramesh tendulkar gives S. R. tendulkar.
  • The capital is made with Character.toUpperCase(ch).
  • A blank line prints INVALID INPUT.
  • Only spaces separate words; titles, hyphens and tabs are not handled.

Algorithm

  1. main reads the line into text with nextLine() and creates ob = new Initials(text). The constructor stores n.trim() in name and sets sur and ini to empty strings.
  2. main calls ob.display(). If name.length() is 0 it prints INVALID INPUT; otherwise it calls findSurname() and findInitials().
  3. findSurname(): p = name.lastIndexOf(' ') (-1 for one word); sur = name.substring(p + 1).
  4. findInitials(): end = name.length() - sur.length(). For i from 0 to end - 1, if name.charAt(i) is not a space and (i is 0 or the previous character is a space), add Character.toUpperCase(ch) + ". " to ini.
  5. display() prints ini + sur. For a one-word name end is 0, so ini stays empty.

Program

The class Initials takes the name in its constructor; findSurname(), findInitials() and display() each do one job.

import java.util.Scanner;

/* Class Initials prints a person's full name with the initials of all the
   words except the last, and the last word (surname) in full.
   The name is passed to the constructor.
   Example: Mohandas Karamchand Gandhi  ->  M. K. Gandhi */
public class Initials
{
    String name;     // stores the full name, without leading or trailing spaces
    String sur;      // stores the last word of the name (the surname)
    String ini;      // stores the initials, each followed by ". "

    /* Parameterised constructor. Parameter n: the full name typed by the user.
       Removes the spaces at the beginning and the end using trim() and stores
       the name in name; the other data members are initialised. */
    Initials(String n)
    {
        name = n.trim();
        sur = "";
        ini = "";
    }

    /* Finds the surname: the part of name after the last space.
       If name has no space (a single word) the whole name is the surname. */
    void findSurname()
    {
        int p = name.lastIndexOf(' ');       // position of the last space, -1 if none
        sur = name.substring(p + 1);         // p = -1 gives the whole name
    }

    /* Builds the initials of every word before the surname.
       A word starts at a non-space character that is the first character
       or follows a space. Its first letter is converted to capital. */
    void findInitials()
    {
        int i;
        char ch;
        int end = name.length() - sur.length();   // characters before the surname
        ini = "";
        for (i = 0; i < end; i++)
        {
            ch = name.charAt(i);
            if (ch != ' ' && (i == 0 || name.charAt(i - 1) == ' '))
                ini = ini + Character.toUpperCase(ch) + ". ";
        }
    }

    /* Prints the name in the form initials followed by surname.
       A blank name is reported as INVALID INPUT. */
    void display()
    {
        if (name.length() == 0)
            System.out.println("INVALID INPUT");
        else
        {
            findSurname();
            findInitials();
            System.out.println(ini + sur);
        }
    }

    /* main reads the name, creates the object and calls display() */
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the full name: ");
        String text = sc.nextLine();            // whole line, spaces allowed
        Initials ob = new Initials(text);       // object of class Initials
        ob.display();
    }
}

Variable description table

VariableData TypePurpose
nameStringData member: the full name, trimmed
surStringData member: the last word of the name (the surname)
iniStringData member: the initials, each followed by .
nStringParameter of the constructor: the name passed in from main
pintLocal variable of findSurname(): position of the last space (-1 if none)
iintLoop counter in findInitials(): index of the character being examined
chcharLocal variable of findInitials(): the character at index i
endintLocal variable of findInitials(): number of characters before the surname
scScannerReads the name from the keyboard in main
textStringLocal variable of main: the line typed by the user
obInitialsObject created in main with text
argsString[]Command line arguments of main (not used)

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 Mahatma Gandhi is trimmed by the constructor to Mahatma Gandhi (Mahatma at 0 to 6, three spaces at 7 to 9, Gandhi at 10 to 15, length 16). findSurname() gives p = 9 and sur = Gandhi (length 6), so end = 16 - 6 = 10.

ichNon-space and (i = 0 or space before)?ini after this step
0Myes, first characterM.
1 to 6a, h, a, t, m, anoM.
7 to 9spacesnoM.

The loop stops at i = 10. display() prints M. Gandhi. (Typing Mahatma Gandhi with single spaces gives the same output, as in 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."