#java

  • 方法一: 系统函数
      public static void main(String[] args){
          Scanner sc = new Scanner(System.in);
          String str = sc.nextLine();
          String[] s = str.split(" "); //正则表达式实用性更强( str.split("\\s+"))
          int length = s[s.length - 1].length();
          System.out.println(length);
      }
  • 方法二: 反过来打印
      public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          String str = sc.nextLine();
          int length = str.length();
          int count = 0;
          for (int i = length - 1; i >= 0; i--) {
              if (str.charAt(i)==' ') { // 或者 if (str.substring(i, i + 1).equals(" ")) {
                  break;
              }
              count++;
          }
          System.out.println(count);
      }