diff --git a/cs/length-of-last-word.cs b/cs/length-of-last-word.cs new file mode 100644 index 0000000..101bd09 --- /dev/null +++ b/cs/length-of-last-word.cs @@ -0,0 +1,25 @@ +public class Solution { + public int LengthOfLastWord(string s) { + var lastLength = 0; + + var length = 0; + foreach (var c in s) { + if (char.IsWhiteSpace(c)) { + if (length != 0) { + lastLength = length; + } + + length = 0; + continue; + } + + ++length; + } + + if (length != 0) { + lastLength = length; + } + + return lastLength; + } +}