Saturday, May 22, 2010

C++ Strings?

Is there a better way to add the word "not" after the word "and" in the string "Pork and Beans"?





void replaceit (string %26amp;str, int pos)


{





str.insert (pos + 3, " not");





cout %26lt;%26lt; str %26lt;%26lt; endl %26lt;%26lt; endl;








return;


}





pos is equal to the position in which the word "and" was found in the string "pork and beans". Instead of pos + 3, how can I make it so that any length word would fit too? pos is equal to 5, btw

C++ Strings?
You can tokenize the string ( using strtok ) and then add the words back to an output stream ( ostringstream ) - while adding, you can insert the word "not".


No comments:

Post a Comment