# 58. Length of Last Word ###### tags: `C++` `LeetCode` `Easy` ## Code ```c++ #include <string> #include <iostream> using namespace std; int lengthOfLastWord(string s); int main() { string s = "Hello World "; cout << lengthOfLastWord(s) << endl; return 0; } int lengthOfLastWord(string s) { int sLen = s.size(); int len = 0; int lastLen = 0; for(int i = 0; i < sLen; i++) { if(s[i] == ' ') { if(len != 0) lastLen = len; len = 0; } else { len++; } } return len == 0 ? lastLen : len; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up