题目描述:

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

输入描述:

一行字符串,非空,长度小于5000。

输出描述:

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

示例1:

输入:

hello world

输出:

5

代码: 

import java.util.*;
public class Main {
    
     public static void main (String[] args) {
        Scanner in = new Scanner ( System.in );
        String word = in.nextLine();
        int n = word.lastIndexOf (" ");
        if ( n == -1)
            System.out.println( word.length() );
         else {
             String str = word.substring ( n ,word.length()-1 );
             System.out.println(str.length());
         }
    }
}

 

相关文章:

  • 2021-06-03
  • 2022-12-23
  • 2022-02-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-26
  • 2021-11-28
  • 2021-12-08
  • 2022-12-23
  • 2022-03-01
  • 2021-05-24
相关资源
相似解决方案