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

4
  • 1
    cout<<array[c]<<"," does not produce a string; it writes to the standard output. Commented May 31, 2020 at 14:42
  • You need to show more context. You don't really have a string at all in the example. Or at least its not clear. I am not sure that array is a std::string Commented May 31, 2020 at 14:43
  • Here is how you replace part of a std::string http://www.cplusplus.com/reference/string/string/replace/ Commented May 31, 2020 at 14:46
  • Why not do the cout differently in the first place? Commented May 31, 2020 at 15:40

1 Answer 1

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; 
Sign up to request clarification or add additional context in comments.

Comments