0

I am trying a short code that uses an array, I basically want to replace the word hate for love when I call my function WordReplace but I keep printing the same thing:

I don't love c++ I don't love c++

I have tried different things but I am not sure what is wrong

#include <iostream>
#include <string>
using namespace std;

void WordReplace(string*x, int start, int end, string g, string w)
{
   for (int z = start; z <= end; z++)
   {
      if (x[z] == g)
         x[z] == w;

      cout << x[z]<<" ";
   }
}

int main()
{
   string x[4] = {"I", "don't", "hate", "c++"};

   for (int i = 0; i < 4; i++)
   {
      cout << x[i] << " ";
   }
   cout << endl;

   WordReplace(x, 0, 3, "hate", "love");

   cout << endl;

   return 0;
}
5
  • 6
    x[z] == w should be x[z] = w. Read about the difference between = and == in your favorite C++ textbook. Commented Sep 7, 2017 at 15:02
  • It might also help to give your variable more intuitive names. At least g and w don't do it for me. But @IgorTandetnik has your answer. Commented Sep 7, 2017 at 15:06
  • I have tried different things -- Except the std::replace algorithm function, which does the job in one line of code. The point is that if you're writing code that has the feeling that it has to have been done millions of times before (like replacing x with y), then there is more than likely an STL algorithm function that does the job (if not a set of functions). Commented Sep 7, 2017 at 15:10
  • Actually gcc 5.1.0 does not produce any warnings. Reason seems to be that operator is overloaded. When std::string replaced with int warning "statement has no effect" is produced as expected. Commented Sep 7, 2017 at 15:26
  • Try using std::vector<std::string> instead of an array. A lot easier to pass to functions. Commented Sep 7, 2017 at 15:37

3 Answers 3

5

Just use std::replace:

std::string x[] = {"I", "don't", "hate", "c++"};
std::replace( std::begin( x ), std::end( x ), "hate", "love" );

live example

Sign up to request clarification or add additional context in comments.

Comments

2

You have c++. Use proper containers (e.g. std::vector).

#include <string>
#include <vector>
#include <iostream>
using namespace std;

void WordReplace(vector<string> &sentence, string search_string,
             string replace_string) {
    for (auto &word : sentence) {
        if (word == search_string)
            word = replace_string;
    }
}

int main() {
    vector<string> sentence{"I", "don't", "hate", "c++"};

    for (const auto word : sentence)
        cout << word << " ";
    cout << endl;

    WordReplace(sentence, "hate", "love");

    for (const auto word : sentence)
        cout << word << " ";
    cout << endl;

     return 0;
}

or even better, don't reinvent the wheel

std::vector<std::string> x {"I", "don't", "hate", "c++"};
std::replace( x.begin(), x.end(), "hate", "love" );

Comments

1

If you want to assign a new value to a variable you need the following syntax:

myVar = myValue;

This will change the value of myVar to myValue.

This construction:

myVar == myValue

is a comparison and is treated as a bool, since it returned true(if myVar equals myValue) and False (if they are not equal). The construction doesn't change the value of myVar or myValue.

In your case you need to replace x[z] == w by x[z] = w, as suggested by Igor

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.