Length of Last Word
2015-08-16 18:25
393 查看
Given a string s consists of upper/lower-case alphabets and empty space characters
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 =
return
int lengthOfLastWord(const char *s) {
int index=strlen(s)-1;
int len=0;
while(index>-1&&*(s+index)==' ')
index--;
if(index<0)
return 0;
while(*(s+index)!=' '&&index>=0){
index--;
len++;
}
return len;
}
' ', 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.
int lengthOfLastWord(const char *s) {
int index=strlen(s)-1;
int len=0;
while(index>-1&&*(s+index)==' ')
index--;
if(index<0)
return 0;
while(*(s+index)!=' '&&index>=0){
index--;
len++;
}
return len;
}
相关文章推荐
- 定义常量的三种方法
- HTTP详解-工作原理
- 关于$.Deferred()对象的应用
- GitHub上整理的一些工具,求补充
- 浅谈new与delete
- 关联容器总结
- codeforces 570 D. Tree Requests (dfs)
- (转)系统设计题要考虑的方面
- ehcache集群详解一(支持的集群类型)
- 设计模式基本概念总结(一)
- A7139 无线通信驱动(STM32) 增加FIFO扩展模式,可以发送超大数据包
- 模板函数 为 void maxn(T *,int n)
- java 实现线程的其中两种方法总结
- Hdoj 1258 Sum it up
- android paopao窗口+gridview 的实现底部菜单
- 黑马程序员 单例设计模式
- Java设计模式偷跑系列(六)Singleton模式的建模与实现
- 中国剩余定理 vijos1164 曹冲养猪
- 第4周工作周报
- thinkphp加载 和url_model