JAVA program to find initials of a name

We have approached this problem in two different ways, would encourage you to take a look at bothe the approaches.


Naive Approach :

Step-1 : Finding the last index of white space.

Step-2 : Looping through all the characters before the the last white space.

Step-3 : If a character is a white space, then adding the next character to the initials string. (In order to get the first word make sure to add a white space in front of the name string).

Step-4 : At last adding the last name to the initials string.

Program :

Sample Output :


Better Approach :

Step-1 : Extracting all the words from the name string in to an array.

Step-2 : Looping through all the words in the array before the last word.

Step-3 : Adding the first character of each word in to the initials string.

Step-4 : At last adding the last word to the initials string.

Program :

Sample Output :

Comments