Bird
0
0

You want to find all positions of the substring 'ana' in str = 'banana bandana' and check if it exists. Which code correctly does this?

hard📝 Application Q15 of 15
MATLAB - String Handling
You want to find all positions of the substring 'ana' in str = 'banana bandana' and check if it exists. Which code correctly does this?
Apositions = strfind('ana', str); exists = contains('ana', str);
Bpositions = contains(str, 'ana'); exists = strfind(str, 'ana');
Cpositions = find(str, 'ana'); exists = contains(str, 'ana');
Dpositions = strfind(str, 'ana'); exists = contains(str, 'ana');
Step-by-Step Solution
Solution:
  1. Step 1: Use strfind to get positions

    strfind(str, 'ana') returns all starting indices of 'ana' in the string.
  2. Step 2: Use contains to check existence

    contains(str, 'ana') returns true if 'ana' is found anywhere in str.
  3. Step 3: Verify options

    positions = strfind(str, 'ana'); exists = contains(str, 'ana'); correctly uses both functions. positions = contains(str, 'ana'); exists = strfind(str, 'ana'); swaps roles. positions = strfind('ana', str); exists = contains('ana', str); reverses arguments. positions = find(str, 'ana'); exists = contains(str, 'ana'); uses invalid function find for strings.
  4. Final Answer:

    positions = strfind(str, 'ana'); exists = contains(str, 'ana'); -> Option D
  5. Quick Check:

    Positions with strfind, existence with contains [OK]
Quick Trick: Use strfind for positions, contains for true/false [OK]
Common Mistakes:
  • Swapping arguments in strfind or contains
  • Using find instead of strfind
  • Confusing output types of contains and strfind

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More MATLAB Quizzes