检查 char * 类型的字符串是否包含另一个字符串
Check if a string of type char * contains another string
我有一个更大的任务,其中包含这个函数。以下是说明;
定义一个名为 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)
{
}
我的两个主要问题是;
- 在这种情况下,参数如何工作?
- 是否有一个函数可以检查字符串中是否存在字符串?如果没有,我应该如何开始编写此类函数?
基于@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;
}
- C++-字符串是否包含一个带有简单循环的单词
- 将常量字符串添加到非常量字符串是否会给出常量字符串
- 检查字符串是否"null" C++
- 使用XOR查找O(n)-解决方案中的两个字符串是否为变位符
- 检查字符串是否为整数
- 使用 SET(C++) 检查两个给定字符串是否是字谜时出现运行时错误
- 如何检查字符串是否正确以将其转换为 int?
- 程序检查字符串是否只包含 1 和 0?
- 检查字符串是否是拉平德罗姆
- 尝试使用 indexOf 创建一个 if 语句来检查字符串是否包含字符.有一点麻烦
- 通过默认复制构造函数比较 C++ 字符串是否会影响性能,原因为何?
- gtest:检查字符串是否等于两个字符串之一
- 如何检查字符串是否包含所有这些:数字、字母和特殊字符
- 如何检查字符串是否以C++中的"ed"结尾;
- 字符串是否在其构造函数中复制数据
- 检查字符串是否是数字,然后将该数字转换为 int?
- 如何检查一个字符串是否包含多个其他字符串?
- 检查字符串是否仅包含字母
- 如何根据一个特定字母的出现来确定两个字符串是否相等?
- 查找字符串是否包含字符串向量的任何一个元素的最佳方法