当前位置:网站首页>[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 .
边栏推荐
- 奇葩pip install
- Latex warning: citation "*****" on page y undefined on input line*
- Function execution space specifier in CUDA
- [self cultivation of programmers] - Reflection on job hunting Part II
- 查询GPU时无进程运行,但是显存却被占用了
- The use of regular expressions in JS
- Shardingsphere JDBC
- Latex error: the font size command \normalsize is not defined problem solved
- Thread hierarchy in CUDA
- js中正则表达式的使用
猜你喜欢
随机推荐
Record RDS troubleshooting once -- RDS capacity increases dramatically
table 组件指定列合并行方法
Kali latest update Guide
20201025 Visual Studio2019 QT5.14 信号和槽功能的使用
20201025 visual studio2019 qt5.14 use of signal and slot functions
Virtualenv and pipenv installation
The default Google browser cannot open the link (clicking the hyperlink does not respond)
Kotlin - 验证时间格式是否是 yyyy-MM-dd HH:mm:ss
web自动中利用win32上传附件
奇葩pip install
ModuleNotFoundError: No module named ‘jieba. analyse‘; ‘ jieba‘ is not a package
DeprecationWarning: .ix is deprecated. Please use.loc for label based indexing or.iloc for positi
Latex 报错 LaTeX Error: The font size command \normalsize is not defined问题解决
Stress test modification solution
js中对于返回Promise对象的语句如何try catch
VSCODE 安装LATEX环境,参数配置,常见问题解决
CUDA and Direct3D consistency
js中正则表达式的使用
Overload global and member new/delete
Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件