Accept a name and print only the initials with surname.
softetechnologies
09-06-2018    7602 times
initials surname.

Write a programme in java to accept a name and print only the initials with surname.

Split the given sting by space (" ") character. and store into a string array. Now get the first character of each word, except last word, followed by a dot (.) character. Now print the last word by convert the first character into uppercase and others characters into lower case.

import java.util.*;
public class Initial
{
    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter A String : ");
        String str=sc.nextLine();
        String res="";
        str=str.toUpperCase();
        String []words=str.split(" ");
        int i;
        for(i=0;i<words.length-1;i++)
        {
            res += words[i].charAt(0) + ".";
        }
        res +=words[i].charAt(0) + words[i].substring(1).toLowerCase() + ".";
        System.out.println(res);
    }
}

Output

Enter A String : mahanDas kAram chanD ganDHi
4M.K.C.Gandhi.

softetechnologies
softetechnologies
    Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
    Related Post
  Query About the post