Friday, 18 June 2021

Write a program to accept the name and display the initials along with the surname.

Sample Input  : Mohandas Karamchand Gandhi
Sample Output : M.K.Gandhi


import java.util.*;
public class Surname_Tridib
{
public static void main(String args[])
{
String st,sn,st1="",st2="";
int i,p;
char ch;
Scanner in = new Scanner(System.in);
System.out.println("Enter the name:");
st = in.nextLine();
st=' '+st;
p=st.lastIndexOf(' ');
sn=st.substring(p);
for(i=0;i<p;i++)
{
    ch=st.charAt(i);
    if(ch==' ')
    st1=st1+st.charAt(i+1)+'.';
    }
st2=st1+sn;
System.out.println(st2);
}
}



Output:






No comments:

Post a Comment