4. Write a program in Java to accept a name containing three words and display the
surname first, followed by the first and middle names
Sample Input: MOHANDAS KARAMCHAND GANDHI
Sample output: GANDHI MOHANDAS KARAMCHAND
Art
Answers
Answered by
2
import java.util.*;
import java.lang.*;
import java.io.*;
class Program
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner Sc = new Scanner(System.in);
String s;
System.out.print("Enter a name of three words : ");
s = Sc.nextLine();
int last_space = s.lastIndexOf(' ');
String last_name = s.substring(last_space + 1);
String initial_name = s.substring(0, last_space);
System.out.println(last_name + " " + initial_name);
}
}
Similar questions