0

if I have a string eg

2,4,-1,3,-1,

how do I replace all the -1's with the word "wrong"? and how do I remove the comma at the very end? the output has come from an array

cout<<array[c]<<",";

I need a basic solution please thank you

1 Answers1

0

Hard to understand what you are trying to say, better if you edit your question. You could do something like this (stackoverflow answer),

  std::string subject("2,4,-1,3,-1,");
  std::string search("-1");
  std::string replace("wrong");
  size_t pos = 0;

  while ((pos = subject.find(search, pos)) != std::string::npos) 
  {
    subject.replace(pos, search.length(), replace);
    pos += replace.length();
  }

  // Delete last ,
  subject.pop_back();

  std::cout << subject;
Bur
  • 157
  • 2
  • 5