当前位置:网站首页>[daily question] - Huawei machine test 01
[daily question] - Huawei machine test 01
2022-07-02 06:48:00 【Xiexishan】
【 A daily topic 】— Huawei machine test 01
subject :HJ1 The length of the last word in the string
describe
Calculate the length of the last word in the string , Words are separated by spaces , String length is less than 5000.( notes : The end of the string does not end with a space )
Input description :
The input line , Represents the string to be evaluated , Non empty , The length is less than 5000.
Output description :
Output an integer , Represents the length of the last word of the input string .
Example 1
Input :
hello nowcoder
Copy
Output :
8
Copy
explain :
The last word is nowcoder, The length is 8
Ideas :
- Use gets Enter a string
When reading a string :
scanf() With Space、Enter、Tab End one input
gets() With Enter End input ( The space does not end ), Accept spaces , Will abandon the last carriage return !
2. Count from back to front , Until I met Space end .
️ The code is as follows
# define _CRT_SECURE_NO_WARNINGS 1
// Calculate the length of the last word in the string , Words are separated by spaces , String length is less than 5000.( notes : The end of the string does not end with a space )
#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;
}
Welcome to correct ! Pay attention that I'm not lost
Today's rubbish : Lifelong learning is the king .
边栏推荐
- Sentinel rules persist to Nacos
- Asynchronous data copy in CUDA
- Alibaba cloud MFA binding Chrome browser
- Utilisation de la carte et de foreach dans JS
- 如何调试微信内置浏览器应用(企业号、公众号、订阅号)
- apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
- 20201025 Visual Studio2019 QT5.14 信号和槽功能的使用
- Kotlin - verify whether the time format is yyyy MM DD hh:mm:ss
- [daily question 1] write a function to judge whether a string is the string after the rotation of another string.
- js判断数组中对象是否存在某个值
猜你喜欢
随机推荐
Win电脑截图黑屏解决办法
Flask migrate cannot detect db String() equal length change
FE - Eggjs 结合 Typeorm 出现连接不了数据库
Self cultivation of programmers - Reflection on job hunting
js中map和forEach的用法
Code execution sequence with and without resolve in promise
FE - Weex 使用简单封装数据加载插件为全局加载方法
qq邮箱接收不到jenkins构建后使用email extension 发送的邮件(timestamp 或 auth.......)
Detailed definition of tensorrt data format
Vscode installation, latex environment, parameter configuration, common problem solving
浏览器滚动加载更多实现
Vector types and variables built in CUDA
[daily question 1] write a function to judge whether a string is the string after the rotation of another string.
Latex error: the font size command \normalsize is not defined problem solved
Promise中有resolve和无resolve的代码执行顺序
Latex compilation error I found no \bibstyle &\bibdata &\citation command
部署api_automation_test过程中遇到的问题
Kali latest update Guide
查询GPU时无进程运行,但是显存却被占用了
Huawei mindspire open source internship machine test questions









