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 GandhigivesM. Gandhi.Avul Pakir Jainulabdeen Abdul KalamgivesA. P. J. A. Kalam.
Rules followed:
- A single-word name is printed unchanged, as typed (
kalamgiveskalam). - The surname keeps the case typed:
sachin ramesh tendulkargivesS. 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
mainreads the line intotextwithnextLine()and createsob = new Initials(text). The constructor storesn.trim()innameand setssurandinito empty strings.maincallsob.display(). Ifname.length()is 0 it printsINVALID INPUT; otherwise it callsfindSurname()andfindInitials().findSurname():p = name.lastIndexOf(' ')(-1 for one word);sur = name.substring(p + 1).findInitials():end = name.length() - sur.length(). Forifrom 0 toend - 1, ifname.charAt(i)is not a space and (iis 0 or the previous character is a space), addCharacter.toUpperCase(ch) + ". "toini.display()printsini + sur. For a one-word nameendis 0, soinistays 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
| Variable | Data Type | Purpose |
|---|---|---|
name | String | Data member: the full name, trimmed |
sur | String | Data member: the last word of the name (the surname) |
ini | String | Data member: the initials, each followed by . |
n | String | Parameter of the constructor: the name passed in from main |
p | int | Local variable of findSurname(): position of the last space (-1 if none) |
i | int | Loop counter in findInitials(): index of the character being examined |
ch | char | Local variable of findInitials(): the character at index i |
end | int | Local variable of findInitials(): number of characters before the surname |
sc | Scanner | Reads the name from the keyboard in main |
text | String | Local variable of main: the line typed by the user |
ob | Initials | Object created in main with text |
args | String[] | Command line arguments of main (not used) |
Sample input and output
Sample 1
Enter the full name: Mohandas Karamchand Gandhi
M. K. GandhiSample 2
Enter the full name: sachin ramesh tendulkar
S. R. tendulkarSample 3
Enter the full name: Avul Pakir Jainulabdeen Abdul Kalam
A. P. J. A. KalamSample 4
Enter the full name: kalam
kalamSample 5
Enter the full name: Mahatma Gandhi
M. GandhiDry 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.
i | ch | Non-space and (i = 0 or space before)? | ini after this step |
|---|---|---|---|
| 0 | M | yes, first character | M. |
| 1 to 6 | a, h, a, t, m, a | no | M. |
| 7 to 9 | spaces | no | M. |
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
- 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."
- Initials only "Write a program to accept a full name and display only the initials in capital letters, e.g. MKG."