我真的需要我的程序最後部分的幫助。我需要在更大的字符串中找到一個字符串,如果找到了,則返回子字符串的起始位置。從方向:在較大的字符串中搜索字符串
請注意,您的字符串位置從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