Write a program to accept a name (first, middle and last format) of a person by the user and display it in the Short form.
Q Write a program to accept a name (first, middle and last format) of a person by the user and display it in the
Short form.
SOLUTION:
import java.lang.String;
import java.util.Scanner;
class q1
{
public static void main(String args[])
{ String name,temp;
int i,j,count,k;
System.out.println("Enter Your Name: ");
Scanner sc = new Scanner(System.in);
name = sc.nextLine();
name = name.toUpperCase();
count = i = 0;
name = name.trim();
temp = String.valueOf(name.charAt(0));
while(i < name.length())
{
if(name.charAt(i) == ' ')
{ j = i;
count++;
if(count > 2)
{
System.out.println("Wrong Format");
System.exit(0);
}
if(count == 1)
{
temp = temp.concat(".");
j++;
temp = temp.concat(String.valueOf(name.charAt(j)));
}
else
{
temp = temp.concat(".");
j = j + 1;
temp = temp.concat(name.substring(j));
}
}
i = i +1;
}
System.out.println(temp);
}
}
OUTPUT:
Comments
Post a Comment