408,432 views
6 votes
6 votes
C++: Given three strings on separate lines, insert the contents of the second string in the first string right before the third string. Then,

output the result

Ex: If the input is:
FuzzyBear

Wuzzy

Be

the output is:
FuzzyWuzzyBear

Note: Using a pre-defined string function, the solution can be just one line of code.

(I tried using strVar.insert(partStr, str2) but was unsuccessful. I don’t know where I’m messing up. Thank you for your help. I’ve been trying to alter this code for a while now)

User Amiregelz
by
2.9k points

1 Answer

14 votes
14 votes

Answer:

Use the .find() function in your first string to give you an index.

Step-by-step explanation:

Hi! I'm guessing you're taking COP2334 like me. A T.A. helped me figure out what I needed, so I'm passing it on.

Basically, you're given three strings and no integer to use as an index in order to insert the second line. Since you are required to have an index to use the .insert() function, this is "tear-out-my-hair" frustrating. What I failed to remember was that it is totally possible to use a function inside another function to get what you need!

You have been given is a hint as to where str2 goes, in the form of your 3rd string. If I'm right, all of the tests the system will run (mine is in Zybooks) will give a 3rd string that is inside the first string.

String 1: strVar: "FuzzyBear"

String 2: str2: "Wuzzy"

String 3: partStr "Be"

Open your statement with strVar and use the insert.() function. In the insert function, you are required to give a place to start, (your index), then what you want to insert. Your index will be the .find() function, searching in StrVar for partStr. The second part of the .insert() function is str2.

Hope that helps!

User Nuvio
by
2.5k points