Kodewick
開始學 →
小挑戰←→0 / 86

書架標籤

#021小挑戰title[i - 1] == ' '約 6 分鐘

圖書角的書架標籤上,書名每個詞的第一個字母要大寫。寫 title_case(title):詞和詞之間是一個空格,每個詞的開頭變大寫,別的字不變。空的就返回空的。

示例
title_case("the little prince")→'The Little Prince'
title_case("")→''
函式string title_case(string title)
main.cpp
#include <string>
#include <cctype>
using namespace std;

string title_case(string title) {
    for (int i = 0; i < title.size(); i++) {
        // 在這裡寫:是開頭或者前一個是空格,就把 title[i] 變大寫
    }
    return title;
}
Ctrl + Enter 也能執行
結果

寫好了先點「執行」試試示例,沒問題再點「提交」。