Write a java program to shrink a name.

Write a program that accepts a string as input and converts the first two names into dot-separated initials and print the output.

Input string format is ‘fn mn ln’.   Output string format is ‘ln [mn’s 1st character].[fn’s 1st character]’

Input and Output Format:

Input consists of a string.

The output consists of a String.

Refer sample output for formatting specifications.

Sample Input:

Sachin Ramesh Tendulkar

Sample Output:

Tendulkar R.S

Name Shrinking in Java.

import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.nextLine();
    getvalues(s1);
  }

  public static void getvalues(String s1) {
    StringBuffer sb = new StringBuffer();
    StringTokenizer st = new StringTokenizer(s1, " ");
    String s2 = st.nextToken();
    String s3 = st.nextToken();
    String s4 = st.nextToken();
    sb.append(s4).append(" ");
    sb.append(s3.substring(0, 1));
    sb.append(".");
    sb.append(s2.substring(0, 1));
    System.out.println(sb);
  }
}

Add a Comment

Your email address will not be published. Required fields are marked *

five × 1 =