Short Name Format Program in Java


import java.util.Scanner;

public class ShortNameFormat
{
    public static void main(String[] args)
    {
        String st,wd="",shortSt="";
        int l;
        Scanner sc=new Scanner(System.in);
        
        System.out.print("Enter String: ");
        st=sc.nextLine();
        
        st=st+" ";
        
        l=st.length();
        for (int i = 0; i < l; i++)
        {
            if(st.charAt(i)==' ')
            {
                if(i==l-1)
                {
                    shortSt=shortSt+wd;
                }
                else
                {
                    shortSt=shortSt+wd.charAt(0)+". ";
                }                
                wd="";
            }
            else
            {
                char ch;
                ch=st.charAt(i);
                wd=wd+ch;
            }
        }
        System.out.println(shortSt);
    }
}


Output:

Enter String: Afaq Ahmad Khan
A. A. Khan
java java tutorials learn java study java