Friday, 6 January 2017

Write a program to find the following pattern ?

Question : Write a program to find the following pattern?

input : Ashok Kumar Yadav
Output: A.K. Yadav

Solution : 
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
      String str1,str2;
      int start=0,i;
      Scanner sc=new Scanner(System.in);
      System.out.print("Enter Your Full Name>> ");
      str1=sc.nextLine();
      for(i=0;i<str1.length();i++)
      {
          if(str1.charAt(i)==' ')
          {
              str2=str1.substring(start,i).trim();
              System.out.print(str2.charAt(0)+".");
              start=i;
          }
      }
      //Printing Last Word
      System.out.print(" "+str1.substring(start,i));
    }
}

No comments:

Post a Comment