Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 8 - More on Strings and Special Methods - Programming Exercises - Page 264: 8.5

Answer

code

Work Step by Step

# 8.5 (Occurrences of a specified string) Write a function that counts the occurrences of a # specified non-overlapping string s2 in another string s1 using the following header: # def count(s1, s2): # For example, count("system error, syntax error", "error") returns # 2. Write a test program that prompts the user to enter two strings and displays the # number of occurrences of the second string in the first string. def count(s1, s2): counter = 0 s1 = s1 + ' ' while len(s1) > 0: s = s1[0: s1.index(' ')] s = s[0:len(s2)] if s == s2: counter += 1 s1 = s1[s1.index(' ')+1: len(s1)] return counter def main(): str1 = input("Enter first string: ") str2 = input("Enter second string: ") c = count(str1, str2) print("String", str2, "is occurred", c, "times in", str1) main()
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.