-2

每当我调用这个函数时,我的程序就会停止工作,但我似乎无法弄清楚为什么。这是我的代码片段:

#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
using namespace std;

void FindReplace(char Memory[], string FindWord, string  ReplaceWord, const int  MAXNUMCHARS)
   {
        int i = 0;
        int SizeWord = FindWord.length();
        int SizeReplace = ReplaceWord.length();

 for(i = 0; i <= MAXNUMCHARS; i++)
   {

       if(Memory[i] == FindWord[i] && Memory[i+1] == FindWord[i+1] && Memory[i+2] ==        FindWord[i+2])
       {
             Memory[i] = ReplaceWord[i];
             Memory[i+1] = ReplaceWord[i+1];
             Memory[i+2] = ReplaceWord[i+2];
       }

 }
}

这是调用它的代码

if(option == 'F')
{
     FindReplace(Memory, FindWord, ReplaceWord, MAXNUMCHARS);
     OutputMemoryToFile(Memory , Out);//copies array to output file
     cout << "String 'the' has been found and replaced with 'can'";
     cout << endl;
}

请问有什么想法吗?:/

4

3 回答 3

2

据我所知,您的调用代码不能确保Memory,FindWord并且ReplaceWord至少是MAXNUMCHARS+3字符长(不过这是一个猜测,因为您没有发布调用代码)。

于 2013-08-30T08:58:11.100 回答
2

您可能需要break;在您的发现if声明中添加一个;

于 2013-08-30T08:56:57.290 回答
0

您也可以检查 i <= MAXNUMCHARS 越界,但使用 i+2

于 2013-08-30T09:10:56.417 回答