当前位置:网站首页>【每日一题】—华为机试01
【每日一题】—华为机试01
2022-07-02 06:12:00 【谢西山】
【每日一题】—华为机试01
题目:HJ1字符串最后一个单词的长度
描述
计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
输入描述:
输入一行,代表要计算的字符串,非空,长度小于5000。
输出描述:
输出一个整数,表示输入字符串最后一个单词的长度。
示例1
输入:
hello nowcoder
复制
输出:
8
复制
说明:
最后一个单词为nowcoder,长度为8
思路:
- 使用gets输入一个字符串
读取字符串时:
scanf()以Space、Enter、Tab结束一次输入
gets()以Enter结束输入(空格不结束),接受空格,会舍弃最后的回车符!
2.从后往前计数,直到遇到空格结束。
️代码如下
# define _CRT_SECURE_NO_WARNINGS 1
//计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
#include <stdio.h>
#include <assert.h>
int the_length_of_last_word(char* str, int i)
{
int length = 0;
assert(str);
for (; i >= 0; i--)
{
if (str[i] == ' ')
{
break;
}
length++;
}
return length;
}
int main()
{
char a[5000] = {
0 };
gets(a);
int b = strlen(a) - 1;
int lengh = the_length_of_last_word(a, b);
printf("%d", lengh);
return 0;
}
欢迎指正!关注我不迷路
今日垃圾话:终身学习才是王道。
边栏推荐
- LeetCode 40. 组合总和 II
- Google Play Academy 组队 PK 赛,正式开赛!
- No subject alternative DNS name matching updates. jenkins. IO found, the reason for the error and how to solve it
- 如何使用MITMPROXy
- Lambda expressions and method references
- ROS create workspace
- Servlet web XML configuration details (3.0)
- 深入了解JUC并发(二)并发理论
- Shenji Bailian 3.53-kruskal
- Bgp Routing preference Rules and notice Principles
猜你喜欢

Unity shader learning notes (3) URP rendering pipeline shaded PBR shader template (ASE optimized version)

神机百炼3.53-Kruskal

Frequently asked questions about jetpack compose and material you

如何使用MITMPROXy

Linear DP (split)

借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力

Shenji Bailian 3.54-dichotomy of dyeing judgment

Introduce uview into uni app

Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice

从设计交付到开发,轻松畅快高效率!
随机推荐
Picture clipping plug-in cropper js
Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
锐捷EBGP 配置案例
LeetCode 78. subset
Jetpack Compose 与 Material You 常见问题解答
Problems encountered in uni app development (continuous update)
穀歌出海創業加速器報名倒計時 3 天,創業人闖關指南提前收藏!
如何使用MITMPROXy
深入了解JUC并发(一)什么是JUC
sudo提权
Support new and old imperial CMS collection and warehousing tutorials
Pbootcms collection and warehousing tutorial quick collection release
The real definition of open source software
亚马逊aws数据湖工作之坑1
Introduce uview into uni app
让每一位开发者皆可使用机器学习技术
神机百炼3.52-Prim
LeetCode 47. Full arrangement II
如何使用MITMPROXy
New version of dedecms collection and release plug-in tutorial tool