C# Clear example without inbuilt functions


  • 0
    S
        public int LengthOfLastWord(string s) 
        {
            if(string.IsNullOrWhiteSpace(s))
                return 0;
            
            int len = s.Length - 1;
            
            // Right trim
            while(s[len] == ' ')
                len--;
    
            int index = len;
    
            while(index >=0)
            {
                if(s[index] == ' ')
                    return (len - index);
                index--;
            }
            
            // If the string has only single word.
            return len + 1;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.