Advertisements
Advertisements
Question
Initials of a name such that the surname appears first followed by the initials:
Input : Vallabh Bhai Patel
Output : Patel VB
Code Writing
Advertisements
Solution
import java.util.*;
class clString
{
public void main()
{
Scanner sc = new Scanner(System.in);
String name = " ";
System.out.print("Enter Name : ");
name = sc.nextLine();
name = " " + name;
int lastsp = name.lastIndexOf(' ');
for(int j = lastsp; j <= name.length() - 1; j++)
{
char ch = name.charAt(j);
System.out.print(ch);
}
for(int j = 0; j < lastsp; j++)
{
char ch = name.charAt(j);
if(ch == ' ')
{
System.out.print(" " + name.charAt(j + 1));
}
}
}
}
Output:
Enter Name : Vallabh Bhai Patel
Patel V В
shaalaa.com
Is there an error in this question or solution?
