设为首页 加入收藏

TOP

leetcode - Length of Last Word
2015-07-20 17:27:00 】 浏览:7786
Tags:leetcode Length Last Word

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.

If the last word does not exist, return 0.

Note: A word is defined as a character sequence consists of non-space characters only.

For example,
Given s = "Hello World",
return 5.

class Solution {
public:
    int lengthOfLastWord(const char *s) {
		int cnt = 0,result = 0;
		while (*s != '\0')
		{
			cnt = *s == ' '  0 : cnt + 1;
			result = cnt > 0  cnt : result;
			s++;
		}
		return result;
    }
};


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇UVA10972 - RevolC FaeLoN(双连通.. 下一篇HDU 4027 Can you answer these q..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目