Java program to print the initial letters of the words present in the String

[2552 views]




This Java program accepts a String from the user and then prints a new String containing the initial letters of the words present in the String which is separated by a dot(.) For Example -
Input: We love Java
Output: W.l.J.

import java.util.Scanner; class PrintInitialLetter { public static void main(String[] args) { String s; System.out.println("Enter String"); Scanner sc=new Scanner(System.in); s=sc.nextLine(); s=" "+s; //add whitespace before first word int l=s.length(); String ans=""; char c='n'; int i; for(i=0;i<l;i++) { c=s.charAt(i); if(c==' ') // check if character is a whitespace { ans=ans+s.charAt(i+1)+"."; } } System.out.println("The final String: "+ans); } }

Output:

Enter String I love Java The final String: I.l.J.
                 



Clear Any Java Interview by Reading our Ebook Once.


Can you clear Java Interview?




Comments










Search Anything:

Sponsored Deals ends in



Technical Quizzes Specially For You:

Search Tags

    Find Initial Letters of words using Java