字符串最后一个单词的长度
描述计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾)
輸入描述输入一行,代表要计算的字符串,非空,长度小于5000。
輸出描述输出一个整数,表示输入字符串最后一个单词的长度。
示例輸入:hello nowcoder 輸出:8 説明:最後一個單詞為nowcoder,長度為8思路
- 編程語言的選取:這是一道C++的測試題目,所以采用C++來編寫
- 設計模式的選擇:個人認爲,在這裏并沒有必要使用單例模式。將類似的功能簡易的工具類設計成單例模式純屬個人習慣。
- 核心問題:解決空格出現在字符串的任意位置的識別問題。
- 容易出現的問題:
(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
測試結果- 由多個單詞構成的字符串
輸入:hello nowcoder 輸出:the last word in string: hello nowcoder is: nowcoder and size is: 8 bytes
- 由單個單詞構成的字符串
輸入:singleword 輸出:the last word in string: singleword is:singleword and size is: 10 bytes
- 全部由空格構成的字符串
輸入: 輸出:Invalid input:A string ends up with space
- 由空格開頭的字符串
輸入: hello 輸出:the last word in string: hello is: hello and size is: 5 bytes
- 由空格結尾的字符串
輸入:hello 輸出:Invalid input:A string ends up with space



