Java bootcamp · Lab 42

Title case

easyStrings12 minLesson: Strings

Read the question, write Java on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read a line; print title case.

Examples

Example 1
Input
hello world
Output
Hello World
Hint
  1. Split words; capitalize first char.
Show correct code

Peek only after you have tried. You can still Check your own version.

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String[] parts = in.nextLine().trim().split("\\s+");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < parts.length; i++) {
      if (i > 0) sb.append(' ');
      String w = parts[i];
      if (w.isEmpty()) continue;
      sb.append(Character.toUpperCase(w.charAt(0)));
      if (w.length() > 1) sb.append(w.substring(1).toLowerCase());
    }
    System.out.println(sb);
  }
}
Main.javaJava 21 · javac · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.