From 49defb257e9a16e96f940a77c24dcbc20116063c Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 1 Apr 2024 21:30:57 +0200 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB58.=20Length=20of=20Last=20Wo?= =?UTF-8?q?rd=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/length-of-last-word.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 cs/length-of-last-word.cs 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; + } +}