Trying to replace words in a string
Solution 1
There is much easier way how to do that:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("Your homework is bad. Really bad.");
while (s.find("bad") != string::npos)
s.replace(s.find("bad"), 3, "good");
cout << s << endl;
return 0;
}
output:
Your homework is good. Really good.
But watch out for case when the needle is a substring of the new value. In that case you might want to be shifting the index to avoid an infinite loop; example:
string s("Your homework is good. Really good."),
needle("good"),
newVal("good to go");
size_t index = 0;
while ((index = s.find(needle, index)) != string::npos) {
s.replace(index, needle.length(), newVal);
index += newVal.length();
}
cout << s << endl;
outputs
Your homework is good to go. Really good to go.
Solution 2
This is the cause of the compilation error:
test2[1]="bad";
test2[1]
is of type char
and "bad"
is of type const char*
: this assignment is not legal.
Use std::string::replace()
to change q
to "bad"
:
test2.replace(i, 1, "bad");
As you also only require to replace the first occurrence of 'q'
(I think this based on the logic in the for
loop) you could replace the for
loop with:
size_t q_idx = test2.find('q');
if (std::string::npos != q_idx)
{
test2.replace(q_idx, 1, "bad");
output2.append(test2);
}
EDIT:
To replace the whole word:
test2 = "bad";
Note, output2
will contain words if they contain 'q'
with the current logic. This would be one way of correcting it:
output2.append(std::string::npos != test2.find('q') ? "bad" : test2);
Comments
-
Jlegend over 3 years
I am trying to take the words from output and find any word with the letter Q in it. If the word does, it needs to be replaced by the word "bad". After that, I am trying to append each word to output2. I am having trouble doing this. The error I get when I compile is:
invalid conversion from 'const char*' to 'char' [-fpermissive]
#include <iostream> #include <string> #include <cstdlib> #include <sstream> using namespace std; string manipulate(string x); int main(int argc, char* argv[]) { string input, temp, output, output2, test, test2; int b; cout << "Enter a string: "; getline(cin, input); istringstream iss(input); while (iss >> test) { if(test.length() != 3) { test.append(" ", 1); output.append(test); } } istringstream iss2(output); while (iss2 >> test2) { for(int i = 0; i<test2.length(); i++) { switch(test2[i]) { case 'q': test2[1]="bad"; output2.append(test2); break; } } } cout << "Your orginal string was: " << input << endl; cout << "Your new string is: " << output2 << endl; cin.get(); return 0; }
-
Jlegend over 12 yearsDoing this will only replace that character with the word "bad". I need to replace the whole word.
-
chris over 12 yearsYou can still use
string::find()
to search for spaces and then search each character from one to the next, and just use the originalfind()
position result if you find a 'q'. -
Kafka over 4 yearsthis solution doesn't work if you want to replace by a string that contain the initial string (example "+" -> "+=")
-
LihO over 4 years@Kafka what a nice case! I've updated my answer, thanks