华为机试题3——字符串最后一个单词的长度

题目描述

计算字符串最后一个单词的长度,单词以空格隔开。 如输入:hello world,输出:5。

解答

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String a=sc.nextLine();
        System.out.println(a.length()-a.lastIndexOf(" ")-1);
    }   
}

猜你喜欢

转载自blog.csdn.net/houguofei123/article/details/79705746
今日推荐