IQ

Ad SpeedyAds

Saturday, February 11, 2012

Java Program to print the initials of a name with last name written in full

public class Initials
{
public static void initials(String S)
{
int[]A=new int[S.length()];
int sp=0;
for(int i=0; i if(S.charAt(i)==' ')
A[sp++]=i;

if(sp==0)
System.out.println(S);
else
{

System.out.print(S.charAt(0)+". ");

for(int i=0; i
System.out.print(S.charAt(A[i]+1)+". ");


System.out.print(S.substring(S.lastIndexOf(' ')+1, S.length()));
}
}
}

7 comments:

  1. Sir,
    for loops are incomplete and syntax errors are arising while compiling the program.

    ReplyDelete
    Replies
    1. I'm really sorry mate.
      Will get it fixed as soon as possible.
      Your response helped in improving the quality.
      Thank you. :)

      Delete
  2. Here is your Program

    import java.util.*;
    public class nameinitial
    {
    public static void main(String args[])
    {

    // variable to read the string
    String fullname;

    //scanner class to get the user input
    Scanner s1=new Scanner(System.in);
    System.out.println("Enter your first middle and last name separted using single

    space");
    fullname=s1.nextLine();

    //splitting the given name on the basis of spaces
    String[] parts=fullname.split(" ");

    // storing 1st character in a character variable
    char f=parts[0].charAt(0);

    // storing 2nd character in a character variable
    char m=parts[1].charAt(0);

    //concatinating the initials and . and last name
    System.out.println(f + "." + m + "." + parts[2]);
    }
    }

    ReplyDelete
    Replies
    1. hey can you do this without arrays and split please?

      Delete
    2. I run the program
      There was an error : could not find the main class

      Delete
  3. can't u do this program using BufferdReader insted of scannr clss.

    ReplyDelete
  4. Can you program in Java to print initials of name in capitals and leaving the last name as same. for example
    Mohandas karmchand ganDhi
    Output shown is --- > M. K. Gandhi

    ReplyDelete