Self-check exercise: Strings and Cell Arrays
-
Write a function that, given two strings
str1,str2and an integerias input arguments, insertsstr2intostr1after itsi-th character. For instance, ifstr1 = 'This exercise is easy.',str2 = 'so'andi = 17the resulting string should be'This exercise is so easy.'. Ifi <= 0ori >= 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 indexifor 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 stringpatappears 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 stringstrand a separating charactersepsplitsstrat the places weresep. The function should return a cell array of strings containing each of the individual pieces of the originalstrbut 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,'}