0

文章を調べて、文字や単語を見つけたら表示するプログラムを作りたいです。

最初の文字/単語を見つけるとすぐに停止するプログラムを考えてみてください。

   string test("This is sentense i would like to find ! "); //his is sentense to be searched
   string look; // word/char that i want to search

   cin >> look;

   for (i = 0; i < test.size(); i++) //i<string size
    {
       unsigned searcher = test.find((look));
       if (searcher != string::npos) {
           cout << "found at : " << searcher;
       }
   }
4

1 に答える 1

1

ループは必要ありません。ただ行う:

std::cin >> look;
std::string::size_type pos = test.find(look);
while (pos != std::string::npos)
{
    // Found!
    std::cout << "found at : " << pos << std::endl;
    pos = test.find(look, pos + 1);
}

入力文字列の結果を示す実際の例を次に"is"示します。

于 2013-03-29T10:53:22.567 に答える