文字列内のすべてのスペースをアンダースコアに置き換えるものを書き込もうとしています。
私がこれまでに持っているもの。
string space2underscore(string text)
{
for(int i = 0; i < text.length(); i++)
{
if(text[i] == ' ')
text[i] = '_';
}
return text;
}
私が何かをしていれば、ほとんどの場合、これはうまくいくでしょう。
string word = "hello stackoverflow";
word = space2underscore(word);
cout << word;
それは「hello_stackoverflow」を出力します。これはまさに私が望むものです。
ただし、次のようなことをする場合
string word;
cin >> word;
word = space2underscore(word);
cout << word;
「こんにちは」という最初の単語を取得します。
誰かがこれの修正を知っていますか?