Invitation to Computer Science 8th Edition

Published by Cengage Learning
ISBN 10: 1337561916
ISBN 13: 978-1-33756-191-4

Chapter 2 - 2.4 - Conclusion - Practice Problems - Page 85: 3

Answer

Pattern = an m$\ \ \ \ $$\ \ \ \ $ = 2. The pattern has 2 characters. Text$\ \ \ \ $ = A man and a woman n$\ \ \ \ $$\ \ \ \ $ = 17. The text has 17 characters. k$\ \ \ \ $i$\ \ \ \ \ $Mismatch$\ \ \ $ Operation 1$\ \ \ \ $1$\ \ \ \ $No$\ \ \ \ $$\ \ \ \ $$\ \ \ \ \ $ Compare $P_{1}$, the “a”, to $T_{1}$, the “A”. No match. $\ \ \ \ $$\ \ \ \ \ \ \ $ Yes$\ \ \ \ $$\ \ \ \ $$\ \ \ \ \ $End of the check for a match at position 1 of the text. 2$\ \ \ \ $1$\ \ \ \ $No$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $ Compare $P_{1}$, the “a”, to $T_{2}$, the blank. No match. $\ \ \ \ $$\ \ \ \ \ \ \ $ Yes$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $End of the check for a match at position 2 of the text. 3$\ \ \ \ $1$\ \ \ \ $No$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $ Compare $P_{1}$, the “a”, to $T_{3}$, the “m”. No match. $\ \ \ \ $$\ \ \ \ \ \ \ $ Yes$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $End of the check for a match at position 3 of the text. 4$\ \ \ \ $1$\ \ \ \ $No$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $ Compare $P_{1}$, the “a”, to $T_{4}$, the “a”. Match. 4$\ \ \ \ $2$\ \ \ \ $No$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $ Compare $P_{2}$, the “n”, to $T_{5}$, the “n”. Match. 4$\ \ \ \ $3$\ \ \ \ $No$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $ $i (3)$ is greater than $m (2)$, so we exit the loop. Output: There is a match at position 4 In a similar way, the program will produce the following two additional lines of output: There is a match at position 7 There is a match at position 16

Work Step by Step

Pattern = an m$\ \ \ \ $$\ \ \ \ $ = 2. The pattern has 2 characters. Text$\ \ \ \ $ = A man and a woman n$\ \ \ \ $$\ \ \ \ $ = 17. The text has 17 characters. k$\ \ \ \ $i$\ \ \ \ \ $Mismatch$\ \ \ $ Operation 1$\ \ \ \ $1$\ \ \ \ $No$\ \ \ \ $$\ \ \ \ $$\ \ \ \ \ $ Compare $P_{1}$, the “a”, to $T_{1}$, the “A”. No match. $\ \ \ \ $$\ \ \ \ \ \ \ $ Yes$\ \ \ \ $$\ \ \ \ $$\ \ \ \ \ $End of the check for a match at position 1 of the text. 2$\ \ \ \ $1$\ \ \ \ $No$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $ Compare $P_{1}$, the “a”, to $T_{2}$, the blank. No match. $\ \ \ \ $$\ \ \ \ \ \ \ $ Yes$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $End of the check for a match at position 2 of the text. 3$\ \ \ \ $1$\ \ \ \ $No$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $ Compare $P_{1}$, the “a”, to $T_{3}$, the “m”. No match. $\ \ \ \ $$\ \ \ \ \ \ \ $ Yes$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $End of the check for a match at position 3 of the text. 4$\ \ \ \ $1$\ \ \ \ $No$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $ Compare $P_{1}$, the “a”, to $T_{4}$, the “a”. Match. 4$\ \ \ \ $2$\ \ \ \ $No$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $ Compare $P_{2}$, the “n”, to $T_{5}$, the “n”. Match. 4$\ \ \ \ $3$\ \ \ \ $No$\ \ \ \ $$\ \ \ \ $$\ \ \ \ $ $i (3)$ is greater than $m (2)$, so we exit the loop. Output: There is a match at position 4 In a similar way, the program will produce the following two additional lines of output: There is a match at position 7 There is a match at position 16
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.