-6

ユーザーが指定した文字列を解析し、古い部分文字列のすべての出現箇所を新しい文字列と交換するにはどうすればよいですか? 操作する機能がありますが、文字列に関しては本当にわかりません。

void spliceSwap( char* inputStr, const char* oldStr, const char* newStr  )
4

2 に答える 2

2

最も簡単な解決策は、ここで google (最初のリンク) を使用することです。また、C++ では よりも優先されることに注意してstd::stringくださいconst char *。独自のものを作成しないでくださいstd::string。組み込みのものを使用してください。あなたのコードはC++ よりも Cに近いようです!

于 2013-07-03T19:33:50.943 に答える
0
// Zammbi's variable names will help answer your  question   
// params find and replace cannot be NULL
void FindAndReplace( std::string& source, const char* find, const char* replace )
{
   // ASSERT(find != NULL);
   // ASSERT(replace != NULL);
   size_t findLen = strlen(find);
   size_t replaceLen = strlen(replace);
   size_t pos = 0;

   // search for the next occurrence of find within source
   while ((pos = source.find(find, pos)) != std::string::npos)
   {
      // replace the found string with the replacement
      source.replace( pos, findLen, replace );

      // the next line keeps you from searching your replace string, 
      // so your could replace "hello" with "hello world" 
      // and not have it blow chunks.
      pos += replaceLen; 
   }
}
于 2013-07-05T07:31:18.507 に答える