Length of Last Word

时间:2022-06-22
本文章向大家介绍Length of Last Word,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1. Description

2. Solution

class Solution {
public:
    int lengthOfLastWord(string s) {
        int length = 0;
        int index = s.find_last_not_of(" ");
        if(index == -1) {
            return length;
        }
        while(index >= 0 && s[index--] != ' ') {
            length++;
        }
        return length;
    }
};