Hello Name Greeting
Problem
Read one line with a name and print the greeting Hello, <name>! Example: for the name Asha print Hello, Asha!
Constraints
- 1 <= length of name <= 100
Examples
Input
Asha
Output
Hello, Asha!
Solutions
We read a line of text, join it with the greeting using the + operator (string concatenation), and print it. Every language has its own way to print, but the idea is the same.
/*
* Problem : Print a greeting for a name.
* Input : One line with a name.
* Output : Hello, <name>!
* Approach: Join the text and the name.
* Time : O(n)
* Space : O(n)
* Java : 8 or newer.
*/
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Step 1: Read one whole line of text. nextLine() also keeps the spaces inside the name.
String name = new Scanner(System.in).nextLine();
// Step 2: Join the text parts with + and print the result.
System.out.println("Hello, " + name + "!");
}
}
Hints (1)
- How do you join two texts?