字符串长度计算

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String words = input.nextLine();
        int index = words.lastIndexOf(" ");
        if (index < 0) {// 找不到空格下标,表示只输入是一个单词
            System.out.println(words.length());
            return;
        }
        String word = words.substring(index + 1);//截取最后一个空格后面的部分字符串
        System.out.println(word.length());
    }
}
全部评论

相关推荐

水墨不写bug:疑似没有上过大学
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务