1

As the title says, how do I replace a string with another string? For example: the user would enter three inputs. The first input is the string that the program would replace; the second is the string that would replace input1; and the third is the string that would be printed out. So if:

Input1 = peanut

Input2 = coconut

Input3 = replacepeanutreplace

Output: replacecoconutreplace

I have started it but my program can only replace words with the same length. I tried searching my problem, but I do not understand the given solutions since I am just new at C/C++.

char replacing[100];
char replacement[100];
char original[1000];
int count;

cin >> replacing;
cin >> replacement;

while(! cin.eof())
{
    cin >> original;

    char * pch;


    pch = strstr (original, replacing);

    count = strlen(replacement);
    strncpy (pch, replacement, count);


    cout << original << endl;

}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • 2
    If you want to learn C++, please go and learn C++. Your problem can be found [here](https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string). If you want to learn C, then please learn C only and look up what `strncpy` does. Don't mix it. That way you well end up doing it wrong *both* ways. – nvoigt Jul 25 '17 at 10:17
  • 1
    Try using `std::string`. It has member function to find a substring and replace parts of it. And it is easy to read from an input stream. – Peter Jul 25 '17 at 10:24
  • You may also be interested in [Why is `iostream::eof` inside a loop condition considered wrong?](/q/5605125) – Toby Speight Jul 25 '17 at 10:32
  • We haven't covered `std::string` yet in class, so I don't really know how to use them. I will try to look into it though. Thanks – futilecheese28 Jul 25 '17 at 11:04

2 Answers2

1

What about:

  • You first find (if any) an occurrence of that string
  • Use replace to substitute the occurrence with the second string

Here is something that should work:

    bool replaceFirst(string& input, const std::string& toBeReplaced, const std::string& replacement) {
    size_t start_pos = input.find(toBeReplaced);
    if(start_pos == std::string::npos)
        return false; //substring not found! 

    input.replace(start_pos, toBeReplaced.length(), replacement); //found. now i can replace!
    return true;
}

Since you are using an array of char instead of string you have to make sure that replacing does not lead you out of bound (strings auto-resize for you).

Davide Spataro
  • 7,319
  • 1
  • 24
  • 36
0

The key problem is that strncpy does not allocate (or free) any memory. This means that if replacement is shorter that replacing, part of replacing will not be overwritten. Similarly if replacement is longer it will overwrite beyond the end of replacing.

As already said, you would be better off using std::string (or some other C++ string class like Qt's QString if Qt is your cup of tea).

One other little thing, in general with streams it's best not to just check for eof, rather write something like

while (cin >> original) {

This will terminate if the stream is in any fail state, not just eof.

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43