Given an integer num and a sentence str, perform the following tasks. If num is
ID: 2265945 • Letter: G
Question
Given an integer num and a sentence str, perform the following tasks. If num is even, take the second half of the string and put it in front of the first half. In addition, put a '' where every s' OR 'S' occurs. If num is odd, delete the first word and then reverse str. In both scenarios, store the updated str in the variable updated. You are guaranteed that the length of str is even. You are guaranteed to have at least two words in the sentence. Example 1: >> str = 'Strings are my favorite.' >> num = 1 updated '.etírova f ym era. Example 2: >> str = 'Strings are my favorite.' >> num = 4 updated "my favorite. String$ are " Write your code for Problem 3 here:Explanation / Answer
Matlab Script:
str = 'Strings are my favorite.';
idx2= 1:length(str);%taking the indexs of str to find words based on spaces
num=1;
if mod(num,2)==1
idx = idx2(str==' ');%gives the indexes of spaces in str
newstr = str(min(idx)+1:end);%taking remaining words other than first word
updated = fliplr(newstr);%reversing the resulting string
disp(updated);
else
newstr1 = str((length(str)/2)+1:end);%gives second half
newstr2 = str(1:(length(str)/2));%gives first half
updated = [newstr1 newstr2];%combing them
updated(updated=='S') = '$';%replacing S with $
updated(updated=='s') = '$';%replacing s with $
disp(updated);
end
str = 'Strings are my favorite.';
idx2= 1:length(str);%taking the indexs of str to find words based on spaces
num=2;
if mod(num,2)==1
idx = idx2(str==' ');%gives the indexes of spaces in str
newstr = str(min(idx)+1:end);%taking remaining words other than first word
updated = fliplr(newstr);%reversing the resulting string
disp(updated);
else
newstr1 = str((length(str)/2)+1:end);%gives second half
newstr2 = str(1:(length(str)/2));%gives first half
updated = [newstr1 newstr2];%combing them
updated(updated=='S') = '$';%replacing S with $
updated(updated=='s') = '$';%replacing s with $
disp(updated);
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.