Menu
Coddy logo textTech

Replace in Array

Lesson 12 of 18 in Coddy's Functions in C++: Building Your Own Functions course.

challenge icon

Challenge

Easy

Write a function to replace a string in an array.


The function will take five arguments: an array of strings, the size of the array, the string to be replaced, the new string to replace it with, and a number specifying how many times the replacement should be performed.

Example:

  • Input: {"Coddy", "C++", "GO", "Tech", "C++"}
  • String to replace: "C++"
  • New string: "JavaScript"
  • Count: 1
  • Output: {"Coddy", "JavaScript", "GO", "Tech", "C++"}
     

Example2:

  • Input: {"Coddy", "C++", "GO", "Tech", "C++"}
  • String to replace: "C++"
  • New string: "JavaScript"
  • Count: 2
  • Output: {"Coddy", "JavaScript", "GO", "Tech", "JavaScript"}

 

We assume that all passed arrays have a length of 5.

Try it yourself

#include <iostream>
#include <string>

using namespace std;

void replaceStringInArray(string arr[], string oldString, string newString, int count) {
  
}

All lessons in Functions in C++: Building Your Own Functions