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
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;
cout<<array[c]<<","does not produce a string; it writes to the standard output.std::stringstd::stringhttp://www.cplusplus.com/reference/string/string/replace/coutdifferently in the first place?