diff --git a/SS2301-02/ss2301_02.cpp b/SS2301-02/ss2301_02.cpp index e69de29..6828690 100644 --- a/SS2301-02/ss2301_02.cpp +++ b/SS2301-02/ss2301_02.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include + +const std::set LOWERCASE_WORDS = { + "a", "an", "and", "the", "in", "on", "at", "by", "for", + "with", "about", "against", "between", "into", "through", + "during", "before", "after" +}; + +std::string toLower(const std::string& str) { + std::string result = str; + for(char& c : result) { + c = std::tolower(c); + } + return result; +} + +std::string capitalize(const std::string& input) { + std::stringstream ss(input); + std::string word, result; + + bool isFirstWord = true; + while (ss >> word) { + if (!isFirstWord) { + result += " "; + } + if (isFirstWord || LOWERCASE_WORDS.find(toLower(word)) == LOWERCASE_WORDS.end()) { + word[0] = std::toupper(word[0]); + for (size_t i = 1; i < word.size(); ++i) { + word[i] = std::tolower(word[i]); + } + } else { // 追加 + word = toLower(word); + } + result += word; + isFirstWord = false; + } + + return result; +} + +int main() { + std::string input = "C++ LANGUAGE IS A PROGRAMMING LANGUAGE."; + std::string output = capitalize(input); + std::cout << output << std::endl; // C++ Language Is a Programming Language. + + return 0; +}