Mega String Manipulation
How would I be able to manipulate a string so that
1. I can change character x to another character
2. I can add a certain character in between two characters
3. I can change all the characters of a certain type to another character
Code would be useful please...
Or even easier...
How do I assign the contents of a string to a character array which has the same length as the string.
Look at the functions in there. They should answer your first few questions.
std::string str1 = "Hello World!";
char str2[str1.size() + 1];
Well I worked out an easier way to do most of it with an array...but I could only get my array = to the string with extra strange characters on the end.
Will try your code...
1. Make sure that string is not const and call the assignment operator after you index the character. C-strings are easy, just use memset().
2. Use insert() on the string object.
std::string brand("Jello Kitty");
brand = 'H'; // "Hello Kitty"
char cstrbrand = brand.c_str();
memset(cstrbrand, 'J', 1); // "Jello Kitty" again
3. I'm not sure I understand, but I suppose you could just use the replace function if you know where to start. If not you have to find() it first.
Crap, a bit slow huh? At least the code is pretty.
std::string msg("My name is George W. Bush and I approve this message!");
msg.replace(11, 14, "citizen");