Print initials of the input name


 

Print initials of the input name

In this section, you will come to know how to display the initials of the given name.

In this section, you will come to know how to display the initials of the given name.

Print initials of the input name

In this section, you will come to know how to display the initials of the given name. For this, user will have to input the name. This name in the form of string is then split into the words with the split() method and store the first character of every word in the character array except the last word i.e.the surname of a person's name. After that, the name will get displayed in a format.

Here is the code:

import java.util.*;

public class StringEx {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Input Name: ");
		String str = input.nextLine();
		String st[] = str.split(" ");
		char ch[] = new char[st.length - 1];
		for (int i = 0; i < st.length - 1; i++) {
			ch[i] = st[i].charAt(0);
		}
		int len = st.length;
		for (int i = 0; i < ch.length; i++) {
			System.out.print(ch[i] + ".");
		}
		System.out.print(st[len - 1]);
	}
}

Output

Input Name: Lal Bahadur Shastri
L.B.Shastri

Ads