首页 > 试题广场 >

最后一词

[编程题]最后一词
  • 热度指数:1942 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定一个可能由任意数量的字母和空格组成的字符串序列,序列中每个只包含字母,不包含任何空格的子序列称为一个单词。请输出一个序列中最后一个单词的长度。

输入描述:
一个由字母和空格组成的字符串


输出描述:
字符串中最后一个单词的长度
示例1

输入

carpe diem

输出

4
public class LastWordLengthExam {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String next = sc.nextLine();
        System.out.println(next.length() - next.lastIndexOf(' ') - 1);
    }
} 
发表于 2019-07-09 14:27:03 回复(0)
系统内部错误是什么意思啊😒?
发表于 2019-06-30 21:36:52 回复(0)
import java.util.*;
public class Main
{
    public static void main(String[] argv)
    {
        Scanner input_str = new Scanner(System.in);
        String s = input_str.nextLine();
        char[] str = s.toCharArray();
        for(int i=str.length-1; i>=0; i--)
        {
            if (Character.isSpace(str[i]))
            {
                System.out.println(str.length-1-i);
                break;
            }
        }
    }
}

发表于 2019-06-26 16:54:34 回复(0)