#3 in your questions to teach you to understand the problem has the wrong answer. If you run the input through your code, it generates a different answer.
Course: Grokking Coding Interview Patterns in C++ - Learn Interactively
Lesson: Replace Words - Grokking Coding Interview Patterns in C++