Bird
0
0

Given text = 'banana bandana', which MATLAB code correctly finds all starting indices of the substring 'ana' within text?

hard📝 Application Q9 of 15
MATLAB - String Handling
Given text = 'banana bandana', which MATLAB code correctly finds all starting indices of the substring 'ana' within text?
Apositions = contains(text, 'ana');
Bpositions = strfind(text, 'ana');
Cpositions = find(text == 'ana');
Dpositions = strfind('ana', text);
Step-by-Step Solution
Solution:
  1. Step 1: Use strfind for substring indices

    strfind returns all starting indices of substring occurrences.
  2. Step 2: Check other options

    contains returns logical, not indices; find cannot compare strings like this; reversed arguments in positions = strfind('ana', text); are incorrect.
  3. Final Answer:

    positions = strfind(text, 'ana'); -> Option B
  4. Quick Check:

    Indices required, only strfind returns them correctly [OK]
Quick Trick: strfind returns all substring start indices [OK]
Common Mistakes:
  • Using contains expecting indices
  • Using find on strings incorrectly
  • Swapping arguments in strfind

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes