Question
Write a program in Java to accept a name and display the initials of first and middle name and full last name.
example 1:
Input: Laxmi Narayan Sharma
output:L N Sharma
example2:
Input:Akash Sharma
output:A Sharma
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class program
{
public static void main(String[] args)
{
String name="",wd="",newname="";
char ch=' ';
int i=0,len=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter full name:");
name = sc.nextLine();
name=name+" ";
len=name.length();
for(i=0;i< len;i++)
{
ch=name.charAt(i);
if(ch==' ')
{
/* if 'i' is equal to (len-1) then we are at the end of string and variable wd contains last name */
if(i==(len-1))
{
newname=newname+wd;
}
else
{
newname=newname+wd.charAt(0)+" ";
}
wd="";
}
else
{
wd=wd+ch;
}
}
System.out.println("Name with first and middle name with initials and full last name:"+ newname);
}
}
Python
fullName=input("Enter Full Name:")
fullName=fullName+" "
wd=""
newName=""
lengthOfGivenName=len(fullName)
for i in range(0,lengthOfGivenName):
if([i]==' '):
if(i==lengthOfGivenName-1):
newName=newName+wd
else:
newName=newName+wd[0]+" "
wd = ""
else:
wd=wd+fullName[i]
print("Name with first and middle name with initials and full last name:",newName)
Coding Store
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale

ICSE QUESTION PAPER WITH SOLUTION(PROGRAMMING ONLY)
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale

ICSE QUESTION PAPER WITH SOLUTION(PROGRAMMING ONLY)
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale
