Replace in Array
Lesson 12 of 18 in Coddy's Functions in C++: Building Your Own Functions course.
Challenge
EasyWrite 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
1Introduction
Introduction2Beginner-level function
Repeat a StringReverse a StringMax ValueFill stringCenter TextFormat TextMath Operation3Intermediate-level functions
Compare two StringsSwap CasesCount Alphabetic LetterReplace in ArrayExtracts Integers Generate CharactersDeletes Specific Index