题目描述:
计算字符串最后一个单词的长度,单词以空格隔开。
输入描述:
一行字符串,非空,长度小于5000。
输出描述:
整数N,最后一个单词的长度。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext())
{
String str = scan.nextLine();
System.out.println(lengthOfLast(str));
}
}
public static int lengthOfLast(String s)
{
String[] strs = s.split(" ");
return strs[strs.length - 1].length();
}
}