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

3

我有一个更大的任务,其中包含这个函数。以下是说明:

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

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

例如:isPartOf("heart", "hypertensive heart disease")返回true,isPartOf("screw", "Case Involving wheelchair")返回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)
{

}

我在这里有两个主要问题:

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

你在顶部的声明与底部的定义不匹配。参数类型应该相同。 - David G
简单解决方案:循环遍历两个字符串。当您在一个字符串中找到与另一个字符串匹配的字符时,增加计数变量。如果没有找到,则将其重置为零。在循环结束时,检查计数是否等于另一个字符串的长度。 - David G
在C语言中,可以使用strstr函数。建议查看Boyer-Moore算法或访问http://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_string_matching_algorithm。 - cup
当您输入单词和句子时,您在哪里分配内存来保存字符?您已经声明了指针,但尚未使它们指向任何内容。cin函数将输入字符并将它们放置在指针Word所指向的位置。在输入之前,它指向哪里? - Thomas Matthews
4个回答

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;
}

1
我真的需要一个在Windows内核模式驱动程序中使用的函数。谢谢你! - Samuel Tulach

4

由于这是 C++,最简单的解决方案是使用 string。你不能像你试图做的那样以字符数组的形式 cin 输入(那段代码并不会做你想要的事情),因此这也解决了你的输入问题:

std::string Word, Sentence;
cout << "Please enter a word: ";
std::getline(std::cin, Word);
cout << endl << "Please enter a sentence: ";
std::getline(std::cin, Sentence);
cout << endl;

if (isPartOf(Word, Sentence)) { 
    // ...
}

另一个关于字符串的好处是它使得isPartOf()非常简单:
bool isPartOf(const std::string& word, const std::string& sentence) {
    return sentence.find(word)    // this returns the index of the first instance
                                  // word
           != std::string::npos;  // which will take this value if it's not found
}

或者,它可以使用strstr实现:

    return strstr(sentence.c_str(), word.c_str());

这就是问题所在,我相信他们不想让我使用字符串,这就是为什么我觉得这很困难的原因。 - Pejman Poh

1

char* 是指向字符串中第一个字符的内存地址的指针。当你首次声明 char* 时,它并没有设置内存地址,因此无法在其中存储任何数据。所以你需要为该 char* 分配内存,以便开始在其中存储数据。例如:

word = (char*) malloc(number_of_bits * sizeof(char));

记住,使用malloc需要包含stdlib.h头文件。

#include <stdlib.h>

一旦您有空间开始存储数据在char*中,您就可以使用cin读取数据。
另外,当您将指针传递给另一个函数时,您需要确保将char*传递给的参数也是char*类型。
void isPartOf(char *a, char *b){
    ...
}

最后,为了确定另一个字符串是否包含子字符串,我会使用strstr函数。
bool isPartOf(char *a, char *b){
   if(std::strstr(b,a) != NULL){    //Strstr says does b contain a
      return true;
   } 
   return false;
}

0

试试这个:

#include <iostream>
#include <string.h>

using namespace std;

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

    for(i;i < strlen(w1); i++)
    {
        if(w1[i] == w2[j])
        {
            j++;
        }
    }

    if(strlen(w2) == j)
        return true;
    else
        return false;
}

int main()
{

    char wrd1[] = "As I know there is a function in C++ string which performs substring search: string1.find(string2)";
    char* W1 = wrd1;
    char wrd2[] = "search";
    char* W2 = wrd2;


    if(isPartOf(W1,W2))
        cout << "true";
    else
        cout << "false";

    return 0;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接