首页 > 学院 > 开发设计 > 正文

华为机试(一)

2019-11-06 08:13:28
字体:
来源:转载
供稿:网友

字符串最后一个单词的长度

计算字符串最后一个单词的长度,单词以空格隔开。

输入描述: 一行字符串,非空,长度小于5000。

输出描述: 整数N,最后一个单词的长度。

输入例子: hello world 输出例子: 5

import java.util.Scanner;public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String str=scanner.nextLine(); System.out.PRint(getLength(str)); } public static int getLength(String str){ if(str==null){ return -1; } String[] strings = str.split(" "); String strLast=strings[strings.length-1]; char[] chars = strLast.toCharArray(); int lens = chars.length; return lens; }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表