c++についてです。以下のコードを使って、文字列の置換をしたいのですが、sourceにab cと入力したいのですが、スペースが入るせいでスペースの後ろのcが次の入力を待機しているfindへ入ってしまうのです。
sourceを入力するときにスペースが入力されても、sourceに入力し続けるにはどうすればいいのでしょうか。
c++
1#include <iostream> 2#include <string> 3 4using namespace std; 5 6string replaceOtherStr(string &replacedStr, string from, string to) { 7 const unsigned int pos = replacedStr.find(from); 8 const int len = from.length(); 9 10 if (pos == string::npos || from.empty()) { 11 return replacedStr; 12 } 13 14 return replacedStr.replace(pos, len, to); 15} 16 17int main() { 18 string source; string find; string replace; 19 cin >> source >> find >> replace; 20 21 22 23 replaceOtherStr(source, find, replace); 24 25 cout << source << endl; 26 27 return 0; 28}
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/18 08:18 編集
2018/11/18 08:36