Wednesday, December 21, 2011

Java program to print the initial letters of the words present in the string entered

/* This Java program accepts a String from the
 * user containing multiple words and then prints
 * a line containing all the initial letters of the
 * words present in the string separated by a dot.
 *
 * Sample Input: My name is Mayank
 * Sample Output: M.n.i.M.
 */

import java.util.*;
import java.lang.Object;
class initialLetter
{
    public static void main()
    {
        String s;
        System.out.println("Enter a String");
        Scanner sc=new Scanner(System.in);
        s=sc.nextLine();
        s=" "+s;
        int l=s.length();
        String ans="";
        char c='n';
        int i;
        for(i=0;i<l;i++)
        {
            c=s.charAt(i);
            if(c==' ')
            {
                ans=ans+s.charAt(i+1)+".";
            }
        }
        System.out.println("The final String: "+ans);
    }
}

//Author : Mayank Rajoria

No comments:

Post a Comment