当前位置:网站首页>[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 .
边栏推荐
- selenium+msedgedriver+edge浏览器安装驱动的坑
- Functions of tensorrt
- Loops in tensorrt
- Vector types and variables built in CUDA
- Présence d'une panne de courant anormale; Problème de gestion de la fsck d'exécution résolu
- Fe - wechat applet - Bluetooth ble development research and use
- Win10桌面图标没有办法拖动(可以选中可以打开可以删除新建等操作但是不能拖动)
- Linux MySQL 5.6.51 community generic installation tutorial
- Tensorrt command line program
- Idea announced a new default UI, which is too refreshing (including the application link)
猜你喜欢
CTF three count
qq邮箱接收不到jenkins构建后使用email extension 发送的邮件(timestamp 或 auth.......)
CTF web practice competition
Latest CUDA environment configuration (win10 + CUDA 11.6 + vs2019)
Solution to the black screen of win computer screenshot
Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决
Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *
The use of regular expressions in JS
AWD学习
Latex error: the font size command \normalsize is not defined problem solved
随机推荐
Latex compiles Chinese in vscode and solves the problem of using Chinese path
Function execution space specifier in CUDA
ModuleNotFoundError: No module named ‘jieba. analyse‘; ‘ jieba‘ is not a package
看完有用的blog
FE - Eggjs 结合 Typeorm 出现连接不了数据库
Sentinel rules persist to Nacos
Dynamic global memory allocation and operation in CUDA
ctf三计
20210306 reprint how to make TextEdit have background pictures
Self study table Au
Storage space modifier in CUDA
Linux MySQL 5.6.51 Community Generic 安装教程
Latex error: the font size command \normalsize is not defined problem solved
Latex在VSCODE中编译中文,使用中文路径问题解决
Asynchronous data copy in CUDA
AWD学习
JS modification element attribute flipping commonly used in selenium's Web Automation
Eggjs -typeorm treeenity practice
selenium+msedgedriver+edge浏览器安装驱动的坑
table 组件指定列合并行方法