2014-11-23 165 views
1

我有一个包含此函数的较大任务。这里是说明;检查char *类型的字符串是否包含另一个字符串

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

实质上,函数应该检查第一个参数指针指向它的字符串 是否是第二个参数指针指向的字符串的一部分。例如:isPartOf(“heart”,“hypertensive heart disease”)返回true isPartOf(“screw”,“Case Involving wheelchair”)返回假回。

我一直在学习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. 是否有函数会检查字符串中是否存在字符串?如果不是,我该如何开始编码这种功能?
+0

你的声明顶部不匹配日底部的定义。参数ypes应该是相同的。 – 0x499602D2 2014-11-23 14:59:57

+0

简单的解决方案:循环两个字符串。当找到与另一个字符串匹配的字符时,请增加一个计数变量。如果不是,则将其重置为零。在循环结束时检查count是否等于另一个字符串的长度。 – 0x499602D2 2014-11-23 15:02:28

+0

答案#2 - 在C中,是:strstr。看看boyer-moore算法或http://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_string_matching_algorithm。 – cup 2014-11-23 17:29:57

回答

2

既然这是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()); 
+0

这就是问题所在,我相信他们不希望我使用字符串,这就是为什么我觉得这很困难。 – 2014-11-23 15:46:08

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

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

char *是指向字符串中第一个字符的第一个内存地址的指针。当你第一次声明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; 
} 
相关问题