-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLengthOfLastWord.cs
More file actions
46 lines (38 loc) · 1.3 KB
/
LengthOfLastWord.cs
File metadata and controls
46 lines (38 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
namespace substrings
{
internal class Program
{
public class Solution
{
public int LengthOfLastWord(string s)
{
string[] substrings = s.Split(' ', StringSplitOptions.RemoveEmptyEntries);
string lastWord = substrings[substrings.Length - 1];
char[] characters = lastWord.ToCharArray();
int lastLength = 0;
if (substrings.Length == 0) return 0;
lastWord = "";
for (int i = 0; i < characters.Length; i++)
{
if (char.IsLetterOrDigit(characters[i]))
{
lastWord += characters[i];
lastLength++;
}
}
Console.WriteLine("Última string: " + lastWord);
return lastLength;
}
}
static void Main(string[] args)
{
Solution solution = new Solution();
Console.Write("\nDigite uma frase: ");
string words = Console.ReadLine();
int length = solution.LengthOfLastWord(words);
Console.WriteLine("\nO comprimento da última string é: " + length);
}
}
}