当前位置:网站首页>【每日一题】—华为机试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;
}
欢迎指正!关注我不迷路
今日垃圾话:终身学习才是王道。
边栏推荐
- Detailed steps of JS foreground parsing of complex JSON data "case: I"
- Linear DP (split)
- JWT tool class
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ F: Polyhedral dice
- 稀疏数组(非线性结构)
- Deep learning classification network -- vggnet
- Replace Django database with MySQL (attributeerror: 'STR' object has no attribute 'decode')
- 来自读者们的 I/O 观后感|有奖征集获奖名单
- 递归(迷宫问题、8皇后问题)
- VRRP之监视上行链路
猜你喜欢
从设计交付到开发,轻松畅快高效率!
Google Play Academy 组队 PK 赛,正式开赛!
Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table
Linear DP (split)
深入了解JUC并发(二)并发理论
In depth understanding of JUC concurrency (I) what is JUC
The difference between session and cookies
注解和反射详解以及运用
经典文献阅读之--SuMa++
VLAN experiment of switching technology
随机推荐
Support new and old imperial CMS collection and warehousing tutorials
Servlet web XML configuration details (3.0)
递归(迷宫问题、8皇后问题)
Invalid operation: Load into table ‘sources_ orderdata‘ failed. Check ‘stl_ load_ errors‘ system table
AttributeError: ‘str‘ object has no attribute ‘decode‘
CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch
Ti millimeter wave radar learning (I)
Pbootcms collection and warehousing tutorial quick collection release
Mock simulate the background return data with mockjs
Page printing plug-in print js
LeetCode 39. 组合总和
神机百炼3.54-染色法判定二分图
Error creating bean with name 'instanceoperatorclientimpl' defined in URL when Nacos starts
从设计交付到开发,轻松畅快高效率!
Detailed explanation of BGP message
Decryption skills of encrypted compressed files
I/o multiplexing & event driven yyds dry inventory
Let every developer use machine learning technology
Use of Arduino wire Library
I/o impressions from readers | prize collection winners list