- #1
gfd43tg
Gold Member
- 950
- 50
Homework Statement
Let sentence1, string1, and string2 be three variables whose values are character strings.
Using only the MATLAB function strfind and array concatenation, write code that will replace
the first occurrence of string1 in sentence1 with string2, assigning the result to variable
sentence2.
sentence1 = 'Prof. Papadopoulos is an E7 instructor';
string1 = 'Papadopoulos';
string2 = 'Packard';
then after executing the correct code, the variable sentence2 will be 'Prof. Packard is an
E7 instructor'.
Homework Equations
The Attempt at a Solution
Here is my code.
Code:
sentence1 = 'Prof. Papadopoulos is an E7 instructor';
string1 = 'Papadopoulos';
string2 = 'Packard';
strfind(sentence1, 'Papadopoulos');
strfind(sentence1, 'is');
size(sentence1);
sentence2 = [sentence1(1:6), string2, sentence1(19:38)];
sentence2
sentence2 =
Prof. Packard is an E7 instructor
Here is the error message I receive when I run the autograder.
Code:
*your code did not execute successfully for the variables: sentence1 = 'Prof. Packard is an E7 instructor'; string1 = 'an E7'; string2 = 'a ME231';
*your code did not execute successfully for the variables: sentence1 = 'Prof. Packard is an E7 instructor'; string1 = 'Prof.'; string2 = 'Dr.';
It appears that the autograder will check the string by replacing with different words, and my code is supposed to work even with word switches. So something about my code only works in this particular case, but if the sentence is changed it is not right in general.
It does give me the sentence, but when I run the autograder, this is the error message.
A hint I got was somehow to incorporate the numel function into my code. That just tells me the number of elements, so I don't see how that will aid me as of yet.
Last edited: