Advertisements
Advertisements
प्रश्न
Initials of a name such that the surname appears first followed by the initials:
Input : Vallabh Bhai Patel
Output : Patel VB
कोड लेखन
Advertisements
उत्तर
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
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
