Takes the first half of any even length word!

mail@pastecode.io avatar
unknown
java
a year ago
612 B
2
Indexable
Never
import java.util.Scanner;

public class Main {
    public static String firstHalf(String str) {
        return str.substring(0, str.length() / 2);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter an even-length word: ");
        String word = scanner.nextLine();

        if (word.length() % 2 == 0) {
            String firstHalf = firstHalf(word);
            System.out.println("First half of the word: " + firstHalf);
        } else {
            System.out.println("The word must have an even length.");
        }
    }
}