[Question:]
Given A string s consists of Upper/lower-case alphabets and empty space characters ' ", return the length of last word in The string.
If The last word does isn't exist, return 0.
Give you a string, try to get the length of the last word of it. If the word does not exist, it returns 0.
[Analysis:] A word is defined as a character sequence consists of Non-space.
For example,
Given s = "Hello World",
Return 5.
[Solution:]
public class Solution {public
int Lengthoflastword (String s) {
if (s!= null &&!s.trim (). Equals ()) {
string[] arr = S.trim (). Split ("");
int length = Arr[arr.length-1].length ();
return length;
}
return 0;
}
public static void Main (string[] args) {
String s = "Leet code";
System.out.println (New Solution (). Lengthoflastword (s));
}
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/