Home
C# Length Of Last Word LeetCode
public class Program
{
//https://leetcode.com/problems/length-of-last-word
static void Main()
{
while (true)
{
Console.WriteLine("Enter the Array");
string myString = Console.ReadLine();
Console.WriteLine(LengthOfLastWord(myString));
}
}
public static int LengthOfLastWord(string s)
{
//trim off empty spaces at the end (and the beginning but that don't matter)
s = s.Trim();
//split into an array from the spaces
string[] myStringArray = s.Split(' ');
//how many items in the array?
int myStringArrayLength = myStringArray.Length;
//get the last item
string myLastWord = myStringArray[myStringArrayLength - 1];
return myLastWord.Length;
}
}
Reader's Comments
Name
Comment
Add a RELEVANT link (not required)
Upload an image (not required)
Uploading...
Home