Question: What is the longest prefix? Write a MATLAB program that reads in two sentences and then identifies the longest common prefix between them. If there

What is the longest prefix? Write a MATLAB program that reads in two sentences and then identifies the longest common prefix between them. If there is no common prefix, then output No common prefix. Note that a prefix is a word, letter or number before another.

Program Requirements 1. You must use a for-loop to step through the sentence of shorter length. Step through one character at a time. You may not use any Matlab shortcuts. 2. If there is no common prefix (i.e., the first letters are different), then output that no common prefix was found. 3. Output both sentences followed by the common prefix. Hint: This program is a perfect example of when to use break. If the characters do not match, break out of the for-loop.

Sample Output

Enter first sentence: A white and brown dog

Enter second sentence: A white cat

Common prefix of the two sentences: A white

>>

Enter first sentence: A white cat

Enter second sentence: One white cat

No common prefix

>>

Any help is appreciated. What I have so far is:

s1 = input('Enter first sentence: ' , 's'); s2 = input('Enter second sentence: ' , 's'); prefix = ''; if length(s1)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!