Write a program to accept the name of a person in first, middle and last name format in a single variable. Then display name in the following format

 

Q Write a program to accept the name of a person in first, middle and last name format in a single variable. Then display name in the following format.

Example : INPUT: MOHANDAS KARAMCHAND GANDHI

                   OUTPUT: GANDHI M.K.

SOLUTION:


import java.lang.StringBuffer;
import java.lang.String;
import java.util.Scanner;
class q7
{
    public static void main(String args[])
    {
         String name;
         String temp1 = new String();
         char p,q,r;
         int j,k = 0,i = 0,count = 0;
         Scanner sc = new Scanner(System.in);
         System.out.println("Enter Name: ");
         name = sc.nextLine();
         name = name.toUpperCase();
         while(i < name.length())
         {
            if(name.charAt(i) == ' ')
            {  
                   count++;
                if(count == 2)
                {
                    temp1 = name.substring(++i);
                    break;
                }    
                else
                    k = i;    
            }
            i++;
         }
         temp1 = temp1.concat(" ");
         temp1 = temp1.concat(String.valueOf(name.charAt(0)));
         temp1 = temp1.concat(".");
        k = k + 1;
         temp1 = temp1.concat(String.valueOf(name.charAt(k)));
        System.out.println(temp1);
    }
}

OUTPUT:



Comments