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

Check if a string of type char * contains another string

本文关键字:字符串 是否 另一个 包含 char 类型 检查      更新时间:2023-10-16

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

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

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

示例:isPartOf ("心脏"、"高血压")心脏病")返回真回是部分("螺丝","涉及的案例"轮椅")返回假背。

我已经学习 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. 是否有一个函数可以检查字符串中是否存在字符串?如果没有,我应该如何开始编写此类函数?

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

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

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

由于这是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)) { 
    // ...
}

string的另一个好处是它使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());

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

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

请记住,malloc 要求您包含 stdlib.h

#include <stdlib.h>

一旦您有空间开始在该字符*中存储数据,您就可以使用 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;
}

试试这个:

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