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 GandhigivesM. K. Gandhi.Mahatma GandhigivesM. Gandhi.Avul Pakir Jainulabdeen Abdul Kalam(extra spaces, with leading and trailing spaces as well) givesA. P. J. A. Kalam.
Rules this program follows:
- Single word: a name of one word is printed as it is, exactly as typed (
KalamgivesKalam,kalamgiveskalam). - Case: initials are always capitals, but the surname is printed in the case typed.
sachin ramesh tendulkargivesS. R. tendulkar, notS. 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 giveD.).
Algorithm
- Read the name into
nameand remove the spaces at its two ends withtrim(). If it is empty, printINVALID INPUTand stop. - Find the surname:
p = name.lastIndexOf(' ')is the position of the last space (-1 if there is none), andsur = name.substring(p + 1)is everything after it. If the name has no space, the whole name is the surname. - Let
end = name.length() - sur.length(), the number of characters before the surname. - Set
ini = "". Forifrom 0 toend - 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". "toini. - Print
inifollowed bysur.
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
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
name | String | The full name typed by the user, without leading or trailing spaces |
p | int | Position of the last space in name, -1 if there is none |
sur | String | The last word of the name, the surname |
ini | String | The initials, each followed by a full stop and a space |
end | int | Number of characters before the surname |
i | int | Loop counter over the characters before the surname |
ch | char | The character being examined |
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 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.
i | ch | Non-space and (i = 0 or space before)? | ini after this step |
|---|---|---|---|
| 0 | M | yes, first character | M. |
| 1 to 7 | o, h, a, n, d, a, s | no | M. |
| 8 | space | no | M. |
| 9 | K | yes, space before | M. K. |
| 10 to 18 | a, r, a, m, c, h, a, n, d | no | M. K. |
| 19 | space | no | M. 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
- 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."
- Capitalise words "Write a program to accept a sentence and convert the first letter of every word to capital."