【问题标题】:Check if a string of type char * contains another string检查 char * 类型的字符串是否包含另一个字符串
【发布时间】:2014-11-23 14:35:06
【问题描述】:

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

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

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

例子:isPartOf("心脏", "高血压 heart disease") 返回 true 返回 isPartOf("screw", "Case Involving 轮椅") 返回错误的返回。

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

【问题讨论】:

  • 您在顶部的声明与底部的定义不匹配。参数类型应该相同。
  • 简单解决方案:遍历两个字符串。当您在一个字符串中找到与另一个匹配的字符时,增加一个计数变量。如果没有,请将其重置为零。在循环结束时检查 count 是否等于另一个字符串的长度。
  • 回答 #2 - 在 C 中,是的:strstr。看看 boyer-moore 算法或en.wikipedia.org/wiki/…
  • 输入时,你在哪里分配内存来保存单词和句子的字符?你已经声明了指针,但没有让它们指向任何东西。 cin 函数将输入字符并将它们放置在指针Word 指向的位置。在你输入之前它指向哪里?

标签: c++ string pointers parameters


【解决方案1】:

由于这是 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());

【讨论】:

  • 这就是问题所在,我相信他们不希望我使用字符串,这就是为什么我觉得这很困难。
【解决方案2】:

基于@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;
}

【讨论】:

  • 我真的需要一个在 Windows 内核模式驱动程序中使用的函数。谢谢你!
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 1970-01-01
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多