当前位置:网站首页>C language force to deduct the length of the last word of the 58th question.Traverse from back to front
C language force to deduct the length of the last word of the 58th question.Traverse from back to front
2022-08-08 04:53:00 【The two dogs will never be rotten】
You are given a string s consisting of several words separated by some space characters before and after them.Returns the length of the last word in the string.
Word is the largest substring consisting of only letters and not containing any space characters.
Example 1:
Enter:s = "Hello World"Output:5Explanation: The last word is "World" and has a length of 5.
Example 2:
Enter:s = " fly me to the moon "Output:4Explanation: The last word is "moon" and has a length of 4.
Example 3:
Enter:s = "luffy is still joyboy"Output:6Explanation: The last word is "joyboy" of length 6.
Tip:
1 <= s.length <= 104sconsists of only English letters and spaces' 'scontains at least one word
The topic is relatively simple in general
The question requires to get the length of the last word in the string. You can traverse the string in reverse, find the last word and calculate its length.
Because at least one word exists in the string, there must be letters in the string.First find the last letter in the string, which is the last letter of the last word.
Continue traversing the string in reverse, starting from the last letter, until a space is encountered or the beginning of the string is reached.Each letter traversed is a letter in the last word, so the number of letters traversed is the length of the last word.
int lengthOfLastWord(char *s){int a = 0; //word lengthint i = 0;int len = strlen(s); //string lengthif(len == 1) return 1;for(i = len - 1; i >= 0; i--){if(s[i] != ' '){a++;if(i > 0)if(s[i - 1] == ' ')break;}}return a;}边栏推荐
- ES6对象字面量的新功能
- 【冷启动】快手《POSO: Personalized Cold Start Modules for Large-scale Recommender Systems》
- 10 must-have free tools for self-media people to operate quickly and efficiently
- 关于如何做选择
- 【Template Engine】velocity
- 全网唯一OpenCyphal/UAVCAN教程(11)用candump和gawk工具写一个Cyphal协议解析小工具
- NetCore uses Dapper to query data
- 由联合体union引出的大小端问题
- Strong Net Cup 2019 - Casual Bet (Stacked Injection)
- 【OAuth2】十八、OIDC的认识应用
猜你喜欢

NetCore使用Dapper查询数据

vulnhub-DC-3 drone penetration record

二维码生成工具

10 must-have free tools for self-media people to operate quickly and efficiently

分布式事务 :可靠消息最终一致性方案

A line of code counts the number of occurrences of the specified string in the text

sqlmap+dnslog注入复现

leetcode-isomorphic string judgment
![[opencv] Introduction to opencv development kit](/img/1a/7b60426d109c9f7231c67e4a4dad46.png)
[opencv] Introduction to opencv development kit

ES6解构赋值的使用说明
随机推荐
leetcode 112. Path sum recursion
The big and small end problem caused by union union
Some excellent blog recommendations for Qt event learning reference
This article will give you a thorough understanding of synchronized and Lock
【论文分享】异质图上的小样本学习:HG-Meta: Graph Meta-learning over Heterogeneous Graphs
L3-005 Litter box distribution
千亿级、大规模:腾讯超大 Apache Pulsar 集群性能调优实践
Flatten multidimensional array to one dimension
[Graph Basics] How to Define Few-Shot Learning on Heterogeneous Graphs: Heterogeneous Graph Few-Shot Learning
Open3D ICP精配准(使用鲁棒性核函数)
Strong Net Cup 2019 - Casual Bet (Stacked Injection)
KDD‘22推荐系统论文梳理(24篇研究&36篇应用论文)
使用ffmpeg解码音频sdl(push)播放
语音鉴定软件
类似Bugfree的9大在线缺陷管理软件
单主机docker 搭建 redis-cluster
力扣84 双周赛 t4 6144 和力扣305周赛t4 6138
awk语法-03-awk表达式(if语句、while循环、for循环)、awk中执行shell命令
【多任务CTR】阿里ESMM:Entire Space Multi-Task Model: An Effective Approach for Estimating Post-Click Conve
Codeforces Round #684 (Div. 1)