Write a program in java to input a name and print its initials except the last name followed by a dot symbol(.).


THE FOLLOWING PROGRAM SEGMENT SHOWS HOW TO PRINT THE INITIALS OF THE NAME EXCEPT THE LAST NAME.

 import java.util.*;

class h
{
    public static void main (String args[])
    {
        Scanner ob = new Scanner (System.in);
        System.out.println("Enter the full name");
        String fn = ob.nextLine();
        String lw = "";
        int l = fn.length(); 
        char ch; int pos = 0;
        for(int i = l-1; i >= 0;i--)
        {
             ch = fn.charAt(i);
            if(ch == ' ')
            {
                pos = i;
                break;
            }
        }
        lw = fn.substring(pos +1, l);
        System.out.print("Output: The initials are ");
        System.out.print(fn.charAt(0)+ ".");
        for(int i = 1; i < pos; i++)
        {
            ch = fn.charAt(i);
            if(ch == ' ')
            {
                System.out.print(fn.charAt(i+1) + ".");
            }
        }
        System.out.print(lw);
    }
}
                
             Hope you guys found it helpful.

Please share it with your friends and leave a comment in the comment section below.

Follow https://askrisfor.blogspot.com/ for more such programs.

Thank you !!!

Visit again.  

        

Comments

Popular Posts