Skip to main content

more options

Self-check exercise: Strings and Cell Arrays

  1. Write a function that, given two strings str1, str2 and an integer i as input arguments, inserts str2 into str1 after its i-th character. For instance, if str1 = 'This exercise is easy.', str2 = 'so' and i = 17 the resulting string should be 'This exercise is so easy.'. If i <= 0 or i >= length(str1) the second string should be inserted at the begining or at the end of str1 , respectively. Make efficient use of MATLAB's vectorized code capabilities, without having to write any loops!

  2. 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 index i for which the i-th position of dna is 'G' and the following two vector components store the letters 'G' and 'T'.

  3. A slight generalization Write a function findPattern(dna, pat) to return a vector of the locations where the string pat appears in dna . 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.

  4. Splitting a string Write a function splitString(str, sep) that given a string str and a separating character sep splits str at the places were sep. The function should return a cell array of strings containing each of the individual pieces of the original str but not including the occurrences of the separating character . For example, if str = 'This problem is relatively easy.' and sep =' ' (a single space) then the cell array returned should be {'This', 'problem', 'is', 'relatively', 'easy,'}