zoukankan      html  css  js  c++  java
  • 计蒜客 挑战难题 最后一个单词的长度

    给定由大写,小写字母和空格组成的字符串,返回最后一个单词的长度。

    如果不存在最后一个单词,返回0

    注意:

       “单词”是指不包含空格符号的字符串

    例如:

       s = “hello World”, 那么返回的结果是5

    格式:

       第一行输入字符串s,然后输出s中最后一个单词的长度。

    样例输入

    Today is a nice day

    样例输出

    3

    =====================================
    第一次code:

     1 import java.util.Scanner;
     2 
     3 public class Main
     4  {  
     5       public static void main (String[] args) 
     6       {  
     7           try
     8           {
     9               Scanner input = new Scanner(System.in);
    10               System.out.println(prime(input.nextLine()));
    11           }
    12           catch(ArrayIndexOutOfBoundsException e)
    13           {
    14               System.out.println(0);
    15           }
    16       } 
    17       public static int prime(String n)
    18       {
    19           int d = 0;
    20           String [] a = n.split(" ");
    21           String b = a[a.length-1];
    22           char [] c = b.toCharArray();
    23           d = c.length;
    24           return d;
    25       }
    26  }

    ==================

    第二次code:

     1 import java.util.Scanner;
     2 
     3 public class Main
     4  {  
     5        public static void main (String[] args) 
     6       {  
     7           Scanner input = new Scanner(System.in);
     8           if (input.hasNext()) 
     9           {
    10                 String s = input.nextLine();
    11                 System.out.println(prime(s));
    12           } 
    13           else 
    14           {
    15                 System.out.println(0);
    16           }
    17       }   
    18       public static int prime(String n)
    19       {
    20           int d = 0;
    21           String [] a = n.split(" ");
    22           String b = a[a.length-1];
    23           char [] c = b.toCharArray();
    24           d = c.length;
    25           return d;
    26       }
    27  }

  • 相关阅读:
    【杭电ACM】1.2.4 Financial Management
    【杭电ACM】1.2.1 Biker's Trip Odometer
    【杭电ACM】1097 A hard puzzle
    【西交ACM】100 A+B problem
    【杭电ACM】1.2.2 Climbing Worm
    【杭电ACM】1004 Let the Balloon Rise
    【杭电ACM】1.2.3 hide handkerchief
    【杭电ACM】1.2.5 find your present (2)
    【杭电ACM】1.2.6 decimal system
    【西交ACM】298 第N大的数
  • 原文地址:https://www.cnblogs.com/niithub/p/5806082.html
Copyright © 2011-2022 走看看