C++ 搜索给定单词的字符串并返回布尔值

C++ 搜索给定单词的字符串并返回布尔值,c++,string,C++,String,我是这个网站的新手,我想问一下是否有人知道如何解决我的问题。 我在网上搜索了几个小时,但没有找到适合我的东西。 任何帮助都将不胜感激 1) 我必须编写一个要求输入单词的函数。 2) 将此单词添加到数组中。 3) 如果单词与给定单词匹配,则搜索字符串。 4) 如果为true或false,则返回布尔值,否则返回布尔值 这里是我到目前为止对函数所做的。所以我相信我已经接近它了(我只需要for循环来搜索单词) bool checkValidTitle(字符串模块[MODULENO+1]){ 字符串数组[

我是这个网站的新手,我想问一下是否有人知道如何解决我的问题。 我在网上搜索了几个小时,但没有找到适合我的东西。 任何帮助都将不胜感激

1) 我必须编写一个要求输入单词的函数。
2) 将此单词添加到数组中。
3) 如果单词与给定单词匹配,则搜索字符串。
4) 如果为true或false,则返回布尔值,否则返回布尔值

这里是我到目前为止对函数所做的。所以我相信我已经接近它了(我只需要for循环来搜索单词)

bool checkValidTitle(字符串模块[MODULENO+1]){
字符串数组[1][20];
cout>数组[1][20];
}

我在一天中写了一个函数,如果第一个字符串包含第二个字符串,那么返回布尔值的是什么:

bool contains(const std::string & str, const std::string substr)
{
    if(str.size()<substr.size()) return false;

    for(int i=0; i<str.size(); i++)
    {
        if(str.size()-i < substr.size()) return false;

        bool match = true;
        for(int j=0; j<substr.size(); j++)
        {
            if(str.at(i+j) != substr.at(j))
            {
                match = false;
                break;
            }
        }
        if(match) return true;
    }
    return false;
}

现在我真的不明白,你想要字符串数组做什么,但是我认为,通过这个,你可以得到想要的输出

这是要求您编写的函数

bool checkValidTitle(string modules[], string word_to_check)
{
  for (int i = 1; i <= MODULENO; ++i)
    if (modules[i] == word_to_check)
       return true;
  return false;
}
bool-checkValidTitle(字符串模块[],字符串字检查)
{

对于(int i=1;您确信您的问题是关于C而不是C++)?您所列的代码是C++代码,而不是C。对于C++,您确实可以使用STD::Stry::FINITS C++:我不知道为什么标题没有显示“++”,谢谢您的快速回复更新:添加了“++”:谢谢你为什么有一个本地数组?你只需要一个字符串。我需要把用户输入的单词放入一个数组中
std::string main_str = "Hello world!";
std::string sub_str = "ello";
std::string sub_str2 = "foo";

bool first = contains(main_str, sub_str); //this will give you true
bool second = contains(main_str, sub_str2); //this will give you false
bool checkValidTitle(string modules[], string word_to_check)
{
  for (int i = 1; i <= MODULENO; ++i)
    if (modules[i] == word_to_check)
       return true;
  return false;
}
string modules[MODULENO+1] = {"", "Maths", "Sciences", "French", "English"};
if (checkValidTitle(modules, "Maths"))
   cout << "Maths is valid\n";
else
   cout << "Maths is not valid\n";
if (checkValidTitle(modules, "Russian"))
   cout << "Russian is valid\n";
else
   cout << "Russian is not valid\n";