栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【華爲機試之C++】HJ1 字符串最後一個單詞的長度

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【華爲機試之C++】HJ1 字符串最後一個單詞的長度

題目

字符串最后一个单词的长度

描述

计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)

輸入描述

输入一行,代表要计算的字符串,非空,长度小于5000。

輸出描述

输出一个整数,表示输入字符串最后一个单词的长度。

示例
輸入:hello nowcoder
輸出:8
説明:最後一個單詞為nowcoder,長度為8
思路
  1. 編程語言的選取:這是一道C++的測試題目,所以采用C++來編寫
  2. 設計模式的選擇:個人認爲,在這裏并沒有必要使用單例模式。將類似的功能簡易的工具類設計成單例模式純屬個人習慣。
  3. 核心問題:解決空格出現在字符串的任意位置的識別問題。
  4. 容易出現的問題:
    (1)在input中不能直接使用std::cin來捕獲輸入,因爲std::cin會自動根據空格來切分字符串。如果輸入"hello world",最後被std::cin捕獲到的是"hello"。爲了獲取完整的字符串,使用getline。
    (2)對find_last_of等接口的理解,這個接口返回的值可以理解成匹配到的字符在字符數組(即字符串)中的索引位置,所以它和長度會相差1,因爲索引值是從0開始計數的。
代碼

StringAnalyzer.h

#pragma once
#include 
#include 
#include 

using namespace std;

class StringAnalyzer
{
public:
    static StringAnalyzer* getInstance();

public:
    
    const string& input();

    
    int8_t verifyValidity(const string& userString);

    
    uint32_t calculateSizeOfLastWord(const string& userString);

    
    void output(const string& userString, uint32_t processerReturn);

private:
    StringAnalyzer() = default;
    ~StringAnalyzer() = default;

private:
    static StringAnalyzer* instance;
    static mutex iMutex;
};

StringAnalyzer.cpp

#include 
#include 
#include "StringAnalyzer.h"

StringAnalyzer* StringAnalyzer::instance = nullptr;
mutex StringAnalyzer::iMutex;


StringAnalyzer* StringAnalyzer::getInstance()
{
    unique_lock lock(iMutex);

    if (nullptr == instance) {
        instance = new StringAnalyzer();
    }
    
    return instance;
}


const string& StringAnalyzer::input()
{
    static string userInput;
    getline(cin, userInput);
    return userInput;
}


int8_t StringAnalyzer::verifyValidity(const string& userString)
{
    
    if (0 == userString.size()) {
        cout << "Invalid input:A empty string" << endl;
        return -1;
    }

    if (5000 < userString.size()) {
        cout << "Invalid input:A string exceeds the limitation of length:5000 bytes" << endl;
        return -2;
    }

    
    auto lastSpacePos = userString.find_last_of(' ');
    //cout << userString << ":" << userString.size() << ";" << userString.substr(0, lastSpacePos + 1) << ":" << userString.substr(0, lastSpacePos).size() + 1 << "." << userString[lastSpacePos-1] << endl;
    if (string::npos != lastSpacePos && userString.size()== lastSpacePos+1) {
        cout << "Invalid input:A string ends up with space" << endl;
        return -3;
    }

    return 0;
}


uint32_t StringAnalyzer::calculateSizeOfLastWord(const string& userString)
{
    auto lastSpacePos = userString.find_last_of(' ');
    
    if (lastSpacePos == string::npos) {
        return userString.size();
    }

    return userString.size() - lastSpacePos - 1;
}


void StringAnalyzer::output(const string& userString, uint32_t processerReturn)
{
    cout << "the last word in string: " << userString << " is:" << (userString.size() -  processerReturn? userString.substr(userString.size() - processerReturn - 1) : userString) << " and size is: " << processerReturn << " bytes" << endl;
}

main.cpp

#include "StringAnalyzer.h"


int main(int argc, char* argv[])
{
    auto stringAnalyzer = StringAnalyzer::getInstance();

    auto userString = stringAnalyzer->input();
    auto verifyRet = stringAnalyzer->verifyValidity(userString);

    if (0 != verifyRet) {
        return -1;
    }

    auto sizeOfLastWord = stringAnalyzer->calculateSizeOfLastWord(userString);

    stringAnalyzer->output(userString, sizeOfLastWord);

    return 0;
}
測試 測試環境

Visual Studio 2019

測試結果
  1. 由多個單詞構成的字符串
輸入:hello nowcoder
輸出:the last word in string: hello nowcoder is: nowcoder and size is: 8 bytes
  1. 由單個單詞構成的字符串
輸入:singleword
輸出:the last word in string: singleword is:singleword and size is: 10 bytes
  1. 全部由空格構成的字符串
輸入:   
輸出:Invalid input:A string ends up with space
  1. 由空格開頭的字符串
輸入: hello
輸出:the last word in string:  hello is: hello and size is: 5 bytes
  1. 由空格結尾的字符串
輸入:hello 
輸出:Invalid input:A string ends up with space
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934089.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号