Home Tutorial Java Core Print initials of the input name

 
 

Share on Google+Share on Google+
Print initials of the input name
Posted on: February 13, 2010 at 12:00 AM
Advertisement
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
Advertisement

Related Tags for Print initials of the input name:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 13, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com