Takes the first half of any even length word!
unknown
java
2 years ago
612 B
8
Indexable
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.");
}
}
}Editor is loading...