题目描述
计算字符串最后一个单词的长度,单词以空格隔开。
输入描述:
一行字符串,非空,长度小于5000。
输出描述:
整数N,最后一个单词的长度。
示例1
输入
hello world
输出
5
public class LengthOfLastWord {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
String data = scanner.nextLine();
String[] split = data.split(" ");
System.out.println(split[split.length-1].length());
}
}
public class LengthOfLastWord {
public static void main(String[] args) throws IOException {
InputStream in = System.in;
int times = 0;
char c = (char) in.read();
while (c !='\n'){
if(c == ' '){
times = 0;
}else{
times++;
}
c=(char) in.read();
}
System.out.println(times);
}
}