检查 char * 类型的字符串是否包含另一个字符串

Pej*_*Poh 3 c++ string parameters pointers

我有一个更大的任务,其中包含此功能。这是说明;

定义一个名为 isPartOf 的 C++ 函数,带有两个指向 C 字符串的参数指针(即 char * 类型,不是来自尚未详细声明的 C++ 数据类型 string ),并返回一个布尔值。

本质上,该函数应该检查第一个参数指针指向它的字符串是否是第二个参数指针指向它的字符串的一部分。

示例: isPartOf(“心脏”、“高血压心脏病”) 返回 true 返回 isPartOf(“螺钉”、“涉及轮椅的案件”) 返回 false。

我已经学习 C 一年了,才开始学习 C++,我发现很难理解“char *”和参数的一般用法。我花了一段时间才理解指针,现在参数让我迷失了。我已经尝试过这段代码,可能包含 * 和 & 的所有可能的迭代,只是为了看看它是否有效,但它不起作用。

#include <iostream>

using namespace std;

void isPartOf(char *, char *);

int main()
{
    char * Word;
    char * Sentence;

    cout << "Please enter a word: ";
    cin >> Word;
    cout << endl << "Please enter a sentence: ";
    cin >> Sentence;
    cout << endl;

    isPartOf(Word, Sentence);

    if (isPartOf(Word, Sentence))
    {
        cout << "It is part of it";
    }
    else
    {
       cout << "It is not part of it";
    }
}

void isPartOf(char a, char b)
{

}
Run Code Online (Sandbox Code Playgroud)

我的两个主要问题是;

  1. 在这种情况下参数如何工作?
  2. 有没有一个函数可以检查字符串中是否存在某个字符串?如果没有,我应该如何开始编写此类函数?

JS5*_*JS5 5

基于@alex.b 代码,我编写了以下几行。我还考虑了禁止使用任何库函数的事实

bool isPartOf(char* w1, char* w2)
{
    int i = 0;
    int j = 0;

    while(w1[i] != '\0')
    {
        if(w1[i] == w2[j])
        {
            int init = i;
            while (w1[i] == w2[j] && w2[j] != '\0')
            {
                j++;
                i++;
            }

            if(w2[j] == '\0')
            {
                return true;
            }
            j = 0;
        }

        i++;
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)