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

华为OJ——字符串最后一个单词的长度

2019-11-08 03:09:21
字体:
来源:转载
供稿:网友

题目:

描述

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

知识点字符串,循环
运行时间限制0M
内存限制0
输入

一行字符串,长度小于128。

输出

整数N,最后一个单词的长度。

样例输入hello world
样例输出5
代码:

The java.util.Scanner.hasNext():This method Returns true if this scanner has another token in its input. This method may block while waiting for input to scan. The scanner does not advance past any input

StringcharAt() Method:This method returns the character located at the String's specified index. The indexes start from zero.

java.util.Scanner.Next():This method returns a String object which is a complete token of the Scanner object.

返回Scanner的下一个标记,或指定模式的下一个匹配,即返回的只是第一个输入

java.util.Scanner.NextLine():  This method returns a String which corresponds to the skipped line of the Scanner object. 

向前跳过当前行,并返回输入中跳过的输入行,即返回的是一行

Example:

public class Test {   public static void main(String args[]) {      String s = "Strings are immutable";      char result = s.charAt(8);      System.out.PRintln(result);   }}Result:

a


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表