我真的需要我的程序最后部分的帮助。我需要在更大的字符串中找到一个字符串,如果找到了,则返回子字符串的起始位置。从方向:在较大的字符串中搜索字符串
请注意,您的字符串位置从0开始,以长度-1结尾。如果找不到字符串,则返回值-1。
我已经开始,下面的代码编译,我只是想知道这是否是正确的。我不想太过头脑,但我需要一些专家的反馈意见。我做对了吗?或者至少我正朝着正确的方向前进?
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;
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;
}
}
SO不适合人们查看您的代码的地方;您可能想要试用Code Review SE站点。 – templatetypedef
与Q没有直接关系:你有内存泄漏:你用'new []'分配(你甚至不需要),但你从不删除[]'。 – jrok
@jrok谢谢!考虑到您指出的泄漏,我编辑了代码。 – user1363061