Sunday, 25 August 2013

How to make sort form of a name using Java.

test2.java
------------------------

import java.util.Scanner;
class test2
{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
String name,name2="";
int i=0,w=1,w1=1,len;
char ch;
System.out.print("\nEnter Full Name:");
name=in.nextLine();
len=name.length();
name2=name.substring(0,1).toUpperCase()+" ";
for(i=0; i<len; i++){
ch=name.charAt(i);
if(ch==' '){
w1=w1+1;
}
}
if(w1>1){
for(i=0; i<len; i++){
ch=name.charAt(i);
if(ch==' '){
w=w+1;
if(w<w1){
name2=name2+name.substring(i+1,i+2).toUpperCase()+" ";
}
else{
name2=name2+name.substring(i+1,i+2).toUpperCase();
i=i+2;
while(i!=len){
name2=name2+name.substring(i,i+1);
i=i+1;
}
}
}
}
}
else{
name2="";
name2=name2+name.substring(0,1).toUpperCase();
for(i=1; i<len; i++){
name2=name2+name.substring(i,i+1);
}
}


System.out.println("Name:"+name2+"\n\n");
}
}

Output:
-------------------------

Enter Full Name:asif mohammad mollah
Name:A M Mollah

1 comment: