HJ1 题解 | #字符串最后一个单词的长度#
字符串最后一个单词的长度
https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da?tpId=37&tqId=21224&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
#include <iostream>
#include<string>
using namespace std;
int Empty(int s); //判断字符是否为空格
int main() {
string word;
getline(cin, word);
int i = 0, j = 0;
int len = word.length();
for (; i < len; i++) {
if (Empty(word[i]))
j = 0;
else
j++;
}
cout << j;
return 0;
}
//与isspace()函数实现一样的思路。
//isspace() 是一个标准的 C 库函数,它位于 ctype.h 头文件中。这个函数用于检查一个字符是否是空白字符。空白字符包括空格、制表符、换行符等。
int Empty(int s){
if (s == 32){ //32是空格的ascll码
return 1;
}
else{
return 0;
}
}
华为机试刷题实录 文章被收录于专栏
记录一下本科应届生(我自己)刷华为机试题的过程
查看1道真题和解析
阿里巴巴灵犀互娱公司福利 647人发布