Length of Last Word
来源:互联网 发布:蒋欣演技知乎 编辑:程序博客网 时间:2024/05/29 12:02
问题:
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
.
int lengthOfLastWord(char* s) {int count = 0;int length = strlen(s); int a = 0,b = length-1; while(s[b]==' ') //找到最后一个词的位置 b--; while(b>=a) { if(s[b] != ' ') //如果没有到最后,就让计数变量count一个字母一个字母的加 count++; else break;//到了最后跳出 b--; }return count;//最后count就是最后一个词的长度}
解析:
这道题其实是给出一个字符窜s,要算出最后一个词的长度,那么我们只需要先利用strlen函数先计算出s的长度,然后定义b=length-1
利用s[b]额来找到最后一个词的 位置,即最后一个空格所在的位置。然后利用好while(b>=0)来让count一个子一个子的相加直到找到最后
s[b] == “”时,跳出,返回计数变量count就是最后一个词的长度。
0 0
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- Length of Last Word
- ORA-01031:insufficient privileges
- iOS10需要在infoPlist文件中配置的权限Key值,包括相册权限 相机权限等
- 英国教授:许多人不理解人工智能到底是什么?
- android群英传笔记 android动画机制
- PHP中isset()和empty()的使用
- Length of Last Word
- 数组指针和指针数组的理解
- FFMPEG deprecated 属性与is deprecated [-Wdeprecated-declarations] 警告
- Scrapy爬虫入门教程十三 Settings(设置)
- 第十六周:104. Maximum Depth of Binary Tree
- k均值聚类算法
- PHP:array_map函数使用
- [DB2]Linux下安装db2 v9.7
- Mysql 运维相关脚本收集