#include <cctype>
class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param s string字符串
     * @return int整型
     */
    int lengthOfLastWord(string s) {
        // write code here
        int ans = 0;
        int n = s.size();
        int flag = 0;
        for (int i = n - 1; i >= 0; --i) {
            if (isalpha(s[i])) {
                ++ans;
                flag = 1;
            } else if (!isalpha(s[i]) && flag)
                break;
        }
        return ans;
    }
};

一、题目考察的知识点

字符串

二、题目解答方法的文字分析

模拟一遍,注意尾句后面也有空格,要去除空格

三、本题解析所用的编程语言

c++