【发布时间】:2012-06-11 02:30:00
【问题描述】:
在我的程序的最后一部分,我真的需要帮助。我需要在更大的字符串中找到一个字符串,如果找到则返回子字符串的起始位置。从方向:
请注意,您的字符串位置从 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;
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;
}
}
【问题讨论】:
-
我想这是家庭作业,这就是你不使用
find的原因,对吗? -
你是对的,这是作业,我们不允许使用实际的查找方法
-
您应该首先描述您的算法应该如何工作,然后将其转换为指令并手动验证它们是否做了他们应该做的事情。
-
今天有不少与字符串相关的作业题
-
3 而一个又一个不是找到子字符串的最佳方式。