すべてのチャレンジ
簡単 0件解決済み 正答率0%

Replace Spaces With Dashes

Turn spaces into dashes to make a URL-style slug.

このチャレンジで学べること

  • 文字列をコピーしながら文字を置き換える
  • URL 形式のスラッグを作る
  • 手書きのループと replace メソッドを比べる

問題

Replace every space in text with a dash -, and convert all letters to lowercase.

Implement slugify_spaces(text).

Input

text: The text to convert.

Output

Return the converted text.

制約

0 ≤ length of text ≤ 10,000

例

例 1
入力:
text = "Hello World"

期待される出力:
expected = "hello-world"
入力:
std::string text = "Hello World";

期待される出力:
std::string expected = "hello-world";
入力:
string text = "Hello World";

期待される出力:
string expected = "hello-world";

解説

The space becomes a dash and letters are lowercased: "hello-world".

例 2
入力:
text = "Learn  To Code"

期待される出力:
expected = "learn--to-code"
入力:
std::string text = "Learn  To Code";

期待される出力:
std::string expected = "learn--to-code";
入力:
string text = "Learn  To Code";

期待される出力:
string expected = "learn--to-code";

解説

Each space becomes its own dash: "learn--to-code".

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

文字列全体を、英小文字化と空白置換の両方を同時に適用して変換する。

ヒント 2

1回の走査で各文字を処理しながら結果を組み立てられる。

ヒント 3

スペース以外の非英字は変えないように注意する。

目安の計算量

時間 O(n)、追加メモリ O(n)

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。