Self-check exercise: Strings and Cell Arrays
-
Write a function that, given two strings
str1
,str2
and an integeri
as input arguments, insertsstr2
intostr1
after itsi
-th character. For instance, ifstr1 = 'This exercise is easy.'
,str2 = 'so'
andi = 17
the resulting string should be'This exercise is so easy.'
. Ifi <= 0
ori >= length(str1)
the second string should be inserted at the begining or at the end ofstr1
, respectively. Make efficient use of MATLAB's vectorized code capabilities, without having to write any loops! -
A bit of DNA analysis The four DNA nucleotides are represented by the letters 'A', 'C', 'G', and 'T'. Write a function
findGGT(dna)
to return a vector of the locations where the substring 'GGT' occurs in dna, an array of characters where each character can only be 'A', 'C', 'G', or 'T'. A "location" where the substring 'GGT' occurs is the indexi
for which thei
-th position of dna is 'G' and the following two vector components store the letters 'G' and 'T'. -
A slight generalization Write a function
findPattern(dna, pat)
to return a vector of the locations where the stringpat
appears indna
. As before, the strings contain only the characters 'A', 'C', 'G', and 'T'. Assume that vector dna is longer than or equal in length to pat. -
Splitting a string Write a function
splitString(str, sep)
that given a stringstr
and a separating charactersep
splitsstr
at the places weresep
. The function should return a cell array of strings containing each of the individual pieces of the originalstr
but not including the occurrences of the separating character . For example, ifstr =
'This problem is relatively easy.' andsep =' '
(a single space) then the cell array returned should be {'This', 'problem', 'is', 'relatively', 'easy,'}