题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da
public class HJ1 { public static void main(String[] args) { boolean flag = true; while (flag) { Scanner scanner = new Scanner(System.in); String next = scanner.nextLine(); if (Objects.equals(next, "-1")) { flag = false; } else { System.out.println(computeStrLengthLastWord(next)); } } } public static Integer computeStrLengthLastWord(String word) { if (!word.contains(" ")) { return word.length(); } String[] splitString = word.split(" "); int index = splitString.length; return splitString[index - 1].length(); } }