zoukankan      html  css  js  c++  java
  • 字符串最后一个单词的长度

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

           你既然已经做出了选择,又何必去问为什么选择?

    背景:Java 在线编程机试刷题。

    题目描述:

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

    输入描述:

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

    输出描述:

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

    示例1

    输入:

    hello word

    输出:

    5

    Java代码:

     1 import java.util.Scanner;
     2 public class Main{
     3     
     4     private static void countLastWordLength(String str){
     5        if(!str.isEmpty()){
     6            String [] split = str.split(" ");
     7            String lastWord = split[split.length - 1];
     8            int length = lastWord.length();
     9            System.out.print(length);
    10        }  
    11     }
    12     
    13     public static void main(String [] args){
    14         Scanner scan = new Scanner(System.in);
    15         while(scan.hasNext()){
    16             String input = scan.nextLine();
    17             countLastWordLength(input);
    18         }
    19     }
    20     
    21 }

    输出验证:

     

     

    你既然已经做出了选择

    又何必去问为什么选择

     

  • 相关阅读:
    CControlLayer
    CBiontCache
    CHero
    CWidgetMgr---cpp
    CWidgetMgr---H
    CXAnimation类
    CXAnimation.h动画类
    CXCommon.h工具类
    【leetcode】441. Arranging Coins
    【linux基础】关于ARM板子使用O3编译选项优化
  • 原文地址:https://www.cnblogs.com/taojietaoge/p/13618896.html
Copyright © 2011-2022 走看看