Search Tutorials

Loading...

Tuesday, 23 April 2013

Java code to print the initials of a Name

Java program to print the initials of a name.

import java.io.*;
class Initials
{
 static String n;
 static int l;
 public static void main(String args[]) throws IOException
 {
 Initials call = new Initials();
 BufferedReader br = new BufferedReader(new
 InputStreamReader(System.in));
 System.out.print("Enter a Name : ");
 n = br.readLine();
 l = n.length();
 call.init();
 }
 public void init()
 {
 int c=0;
 char b=0,d=0;
 String e = "";
 for(int i=0;i<l;i++)
 {
  if(n.charAt(i)==' ')
  {
   c++;
   if(c==1)
   b = n.charAt(i+1);
   if(c==2)
   d = n.charAt(i+1);
   e = n.substring(i+1);
  }
 }
 char f = n.charAt(0);
 System.out.println(f +"." +b +"." +e);
 System.out.println(f +"." +b +"." +d +".");
 System.out.println(e +" " +f +"." +b +".");
 }
}

ALGORITHM:-

1. Start

2. Accept a name from the user.

3. In a two word name, break the name from the first occurring blank-space.

4. In a three word name, break the name from the second occurring blank-space.

5. Print the first letter of each word and the surname.

6. End

OUTPUT:-

Java code to print the initials of a Name

Enter a Name : Mayank K Rastogi

M.K.Rastogi

M.K.R.

Rastogi M.K.

Related Programs:-

Convert a number into words

Reverse a word or sentence ending with '.' using recursion

Convert Decimal to Octal

Convert Decimal to Binary

Print the Factorial of a number using recursion

No comments:

Post a Comment

Back to Top