# 最后一个单词的长度

<p>给你一个字符串 <code>s</code>，由若干单词组成，单词之间用空格隔开。返回字符串中最后一个单词的长度。如果不存在最后一个单词，请返回 0 。</p><p><strong>单词</strong> 是指仅由字母组成、不包含任何空格字符的最大子字符串。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>s = "Hello World"<strong><br />输出：</strong>5</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>s = " "<strong><br />输出：</strong>0</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>1 <= s.length <= 10<sup>4</sup></code></li>	<li><code>s</code> 仅有英文字母和空格 <code>' '</code> 组成</li></ul>
<p>以下错误的选项是？</p>

## aop

### before

```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after

```cpp
int main()
{
    Solution sol;
    string s = "Hello World";
    int res;

    res = sol.lengthOfLastWord(s);
    cout << res;
    return 0;
}
```

## 答案

```cpp
class Solution
{
public:
    int lengthOfLastWord(string s)
    {
        if (s.empty())
            return 0;
        int pos = s.find_last_of(' ');
        while (pos == s.length() - 1)
        {
            if (s.empty())
                return 0;
            s.erase(pos);
            pos = s.find_last_of(' ');
        }
        return s.length() - pos;
    }
};
```

## 选项


### A

```cpp
class Solution
{
public:
    int lengthOfLastWord(string s)
    {
        string word;
        stringstream ss(s);
        while (ss >> word)
        {
        }
        return word.size();
    }
};
```

### B

```cpp
class Solution
{
public:
    int lengthOfLastWord(string s)
    {
        int countWord = 0;
        for (int index = 0; index < s.size(); index++)
        {
            if (s[index] == ' ')
            {
                if (index != s.size() - 1 && s[index + 1] != ' ')
                {
                    countWord = 0;
                    continue;
                }
                else
                    continue;
            }
            countWord++;
        }
        return countWord;
    }
};
```

### C

```cpp
class Solution
{
public:
    int lengthOfLastWord(string s)
    {
        if (s.empty())
            return 0;
        vector<string> word;
        int begin, lenWord = 0;
        for (int i = 0; i < s.size(); i++)
        {
            if ((i == 0 && s[i] != ' ') || (i != 0 && s[i - 1] == ' ' && s[i] != ' '))
            {
                begin = i;
                lenWord++;
                if (i == s.size() - 1)
                {
                    word.push_back(s.substr(begin, lenWord));
                    break;
                }
            }
            else if (s[i] != ' ' && i != s.size() - 1)
            {
                lenWord++;
            }
            else if (i != 0 && s[i - 1] != ' ' && s[i] == ' ')
            {
                word.push_back(s.substr(begin, lenWord));
                lenWord = 0;
            }
            else if (s[i] != ' ' && i == s.size() - 1)
            {
                word.push_back(s.substr(begin, lenWord + 1));
            }
        }
        if (word.empty())
            return 0;
        else
            return word[word.size() - 1].size();
    }
};
```
