Describe |
Calculates the length of the last word in a string, separated by a space. |
Knowledge points |
String, looping |
Run time limit |
0M |
Memory limit |
0 |
Input |
A line of strings that are less than 128 in length. |
Output |
Integer n, the length of the last word. |
Sample input |
Hello World |
Sample output |
5 |
Test OK Code:
ImportJava.util.Scanner; Public classmain{ Public Static voidMain (string[] args) {inti; //System.out.println ("Please enter string:"); Scanner Scstr =NewScanner (system.in);//get a string from the keyboardString str = scstr.nextline ();//take the contents of the scanner object as a string Chararr[]=Str.tochararray (); for(i=arr.length-1;i>=0;i--){ if(arr[i]== ") Break; } intlength=arr.length-i; Charshuchu[]=New Char[Length-1];intK=0; for(intj=i+1;j<=arr.length-1;j++) {Shuchu[k]=ARR[J]; k++; } //System.out.println ("The Last Word is:");System.out.println (k); } }
hw-the length of the last word in a string, separated by a space.