Replacement in c-style strings

We are doing a project using dynamic arrays and command line arguments. The objective is to be able to enter in the command line like this:

./program <file_name> <search_word> <replace_word>

I am totally stuck on the replacement. I understand that you can use strstr to return a pointer to the first location of your searched string, but I do not understand how to implement that into replacing the word within my array.

Here is my current source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

int length(char[]);
void read(char[], char[]);
int replace(char[], char[], char[], char[], int);

int main(int argc, char* argv[])
{
   int size = length(argv[1]); //size variable created to avoid calling function each time needed

   char *origContent = new char[size];//dynamically create an array based off # from length function
   char *modContent = new char[size*2];//dynamically create an array double the size of first array
   read(argv[1], origContent);
   replace(origContent, modContent, argv[2], argv[3], size);


   delete [] origContent;
   delete [] modContent;
}

int length(char fileName[])
{
   char c;//character to be read into
   int count;//counter for # of file elements

   ifstream sampleFile;
   sampleFile.open(fileName);//open the file

   while(sampleFile.get(c))
      count++;

   sampleFile.close();

   return count;

}

void read(char fileName[], char fileContents[])
{
   ifstream sampleFile;
   sampleFile.open(fileName);

   for (int i = 0; !sampleFile.eof(); i++)
      sampleFile.get(fileContents[i]);
}

int replace(char origContent[], char modContent[], char searchWord[], char replaceWord[], int size)
{

   char * stringLoc;
   stringLoc = strstr(origContent, searchWord);//stringLoc points to the very first occurence of the search word in the origContent array

}//I need to know how to use stringLoc to replace the words in origContent!


I am so stumped any help would be appreciated, thanks.
So if the stringLoc holds the address of the first place the substring is found within origContent, how can I write code that will replace that with the replaceWord pointer? I believe if I could just get help with this portion I may be able to finish my project. Also, the searchWord and replaceWord are NOT always the same length.
Topic archived. No new replies allowed.