0
0
MATLABdata~20 mins

String replacement in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Replacement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this string replacement code?
Consider the following MATLAB code that replaces a substring in a string. What will be the output?
MATLAB
str = 'I love apples';
newStr = strrep(str, 'apples', 'oranges');
disp(newStr);
AI love oranges
BI love apples
CI love applesoranges
DI love
Attempts:
2 left
💡 Hint
The function strrep replaces all occurrences of the old substring with the new substring.
Predict Output
intermediate
2:00remaining
What does this code output after replacing multiple substrings?
This MATLAB code replaces multiple substrings sequentially. What is the final output?
MATLAB
str = 'cat bat rat';
str = strrep(str, 'bat', 'dog');
str = strrep(str, 'rat', 'fox');
disp(str);
Acat dog fox
Bcat dog rat
Ccat bat rat
Ddog cat fox
Attempts:
2 left
💡 Hint
Each strrep call replaces all occurrences of the target substring.
Predict Output
advanced
2:00remaining
What is the output of this code using regexprep for string replacement?
This MATLAB code uses regexprep to replace all digits with '#'. What is the output?
MATLAB
str = 'Room 123, Floor 4';
newStr = regexprep(str, '\\d', '#');
disp(newStr);
ARoom #23, Floor 4
BRoom 123, Floor 4
CRoom ###, Floor #
DRoom ###, Floor 4
Attempts:
2 left
💡 Hint
The pattern '\d' matches each digit individually.
Predict Output
advanced
2:00remaining
What error does this code produce when using strrep incorrectly?
What error will this MATLAB code produce?
MATLAB
str = 'hello world';
newStr = strrep(str, 'world');
disp(newStr);
Ahello
BError: Not enough input arguments.
Chello world
DError: Undefined function or variable 'strrep'.
Attempts:
2 left
💡 Hint
Check the number of arguments strrep requires.
🧠 Conceptual
expert
2:00remaining
How many replacements occur in this code using strrep with overlapping substrings?
Given the code below, how many replacements does strrep perform?
MATLAB
str = 'aaaa';
newStr = strrep(str, 'aa', 'b');
disp(newStr);
A1
B4
C3
D2
Attempts:
2 left
💡 Hint
strrep does not replace overlapping occurrences twice.