gpt4 book ai didi

c++ - 在较大字符串中查找子字符串的查找方法

转载 作者:行者123 更新时间:2023-11-30 04:27:22 35 4
gpt4 key购买 nike

<分区>

我的程序的最后一部分真的需要帮助。我需要在一个较大的字符串中找到一个字符串,如果找到则返回子字符串的起始位置。从方向:

Note that your string location starts from 0 and ends at length -1. If the string is not found, a value of -1 will be returned.

我已经开始编译下面的代码,我只是想知道这是否真的正确。我不想太过纠结,但我需要专家的一些反馈。我这样做对吗?或者至少我的方向是正确的?

const int MyString::Find(const MyString& other)
{
int start(0);
int counter(0);
int end = other.Size;
int count(0);
int end1 = Size;
int nfound = -1;

char* temp;
temp = new char[other.Size];

if(other.String[0] != '\0' && other.String[0] != ' ')
{
if(other.String[count] == String[counter])
{
start = counter;

for(int i = count; i < end-1;i++)
{
for(int j = counter; j < end1 -1; j++)
{


temp[j] = String[j];
}
}
if(other == temp)
{
return start;
}
else
return nfound;
}

else{
while(other.String[count] != String[counter])
{
counter++;
if(other.String[count] == String[counter])
{
start = counter;
for(int i = count; i < end-1;i++)
{
for(int j = counter; j < end1 -1; j++)
{


temp[j] = String[j];
}
}
if(other == temp)
{
return start;
}
else
return nfound;
}







}


}

}
else
{
return nfound;
}





}

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com