013-001-009

Substring Extraction and Concatenation

Medium

Problem Description

Substring Extraction and Concatenation

In this problem, you will create a program that reads a full name from standard input, splits it into last name and first name using the substring() method, concatenates them in reverse order, and displays the result.

Learning Objective: Understand how to extract substrings using the substring() method and how to concatenate strings

Overview

In Java's String class, you can extract a part of a string using the substring() method. You can also concatenate strings using the + operator or the concat() method.

Specifications

  1. Read one line of a full name (e.g., "Yamada Taro") using Scanner
  2. Use the substring() method to extract the last name and first name separately
  3. Concatenate the extracted names in reverse order and output in the format "FirstName LastName"

Hints

  • substring(beginIndex) gets from the specified position to the end of the string
  • substring(beginIndex, endIndex) gets the specified range (endIndex is exclusive)
  • The position of the space can be obtained with indexOf(" ")

Input Format

LastName FirstName

Output Format

FirstName LastName

Example

Input: Yamada Taro
Output: Taro Yamada

Test Cases

※ Output examples follow programming industry standards

Normal case
Input:
Yamada Taro
Expected Output:
Taro Yamada
Normal case
Input:
Smith John
Expected Output:
John Smith

Your Solution

Current Mode: My Code
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write your code here

sc.close();
}
}
0 B / 5 MB

You have 10 free executions remaining