Find the length of the last word present in the sentence
Input: s = "Hello World"
Output: 5
Step 1: Remove the extra spaces from the string
Use inbuilt Trim()
function to remove the extra spaces from the string
Step 2 : Convert string to Array
Convert string to the array by using the Split()
function
Step 3: Find last element of Array
Find the last element of the array and find the length of it.
C# Program
public int LengthOfLastWord(string s) {
string [] data=s.Trim().Split(' ');
string lastString=data[data.Length-1];
return lastString.Length;
}
Need help?
Read this post again, if you have any confusion or else add your questions in Community