cs: add «58. Length of Last Word»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
a59eaa2eb4
commit
49defb257e
1 changed files with 25 additions and 0 deletions
25
cs/length-of-last-word.cs
Normal file
25
cs/length-of-last-word.cs
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue