在字符串中搜索特定单词

时间:2014-11-29 12:19:21

标签: windows codeblocks

我想为控制台游戏创建一个非常基本的AI。我需要一个能识别通过cin创建的字符串中的2或3个单词的函数。任何帮助都会非常感激,因为我已经有一段时间了,而且似乎无法找到解决方案。

1 个答案:

答案 0 :(得分:0)

#include <string>
#include <iostream>

using namespace std;

 //-------------------------------------------globals
string sentence;





int main()
{
getline(cin, sentence);

string search;
size_t pos;

search = "hi";
pos = sentence.find(search);
if (pos != string::npos)
    cout << "hello sir " << endl;
else
search = "cya";
    pos = sentence.find("cya");
if (pos != std::string::npos)
    std::cout << "goodbye sir " << endl;
else
search = "goodbye";
    pos = sentence.find("goodbye");
if (pos != std::string::npos)
    std::cout << "shuting down sir " << endl;
else



return 0;
}

我想要的是让初始搜索查找其他单词,例如hello hey等等,以及与其他单词的similer事物

相关问题