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
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务