Print a Card with a Text Block

Problem

Read a name. Print a card with four lines: +----------+, then | Name: <name>, then | Welcome to CodingGyan!, then +----------+.

Constraints

  • The name has 1 to 30 characters

Examples

Input

Asha

Output

+----------+
| Name: Asha
| Welcome to CodingGyan!
+----------+

Solutions

Best approachTime O(n)Space O(n)

Many languages have a multi-line string. Old Java needs \n and plus signs for every line.

Same code works in Java810172125Basic features only, so nothing changes between versions.
/*
 * Problem : Process one line of text.
 * Input   : One line of text.
 * Output  : A 4 line card.
 * Approach: Join lines or use a multi-line literal.
 * Time    : O(n)
 * Space   : O(n)
 * Java    : 8 or newer.
 */
import java.util.Scanner;

public class Main {
    // This method solves the task. It receives the input and returns the answer.
    static String solve(String s) {
        // Step 1: Join the lines with \n. Quotes and plus signs make the code hard to read.
        String name = s.trim();
        return "+----------+\n"
            + "| Name: " + name + "\n"
            + "| Welcome to CodingGyan!\n"
            + "+----------+";
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.hasNextLine() ? scanner.nextLine() : "";
        System.out.println(solve(s));
    }
}
Hints (1)
  1. How do you write a new line in a normal Java string?