题目描述
计算字符串最后一个单词的长度,单词以空格隔开。
输入描述:
输入一行,代表要计算的字符串,非空,长度小于5000。
输出描述:
输出一个整数,表示输入字符串最后一个单词的长度。
示例1
输入
hello nowcoder
输出
8
public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); in.close(); String[] str = s.split(" "); int length = str[str.length - 1].length(); System.out.println(length); }
public static void main(String[] args) throws Exception { InputStream in = System.in; int l; byte[] bytes = new byte[1024]; while ((l= in.read(bytes)) > 0){ String str = new String(bytes, 0, l - 1); String[] strs = str.split(" "); System.out.println(strs[strs.length - 1].length()); } }