1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
kolbaska11 [484]
3 years ago
13

Write a function called count_occurrences that takes two strings. The second string should only be one character long. The funct

ion should return how many times the second string occurs in the first string.

Computers and Technology
1 answer:
iVinArrow [24]3 years ago
8 0

Answer:

Here is the Python function:

def count_occurrences (string1 , string2):  # method that takes two strings as parameter and returns how many times second string occurs in first

   count = 0  #counts number of occurrence of string2 in string1

   for word in string1:  #iterates through each word of the string1

       for character in word:  #iterates through each character of each word in string1

           if character == string2:  # checks if the character in string1 is equal to the character of string2

               count = count + 1  #adds 1 to the count of string2 in string1

   return count  #returns number of times the string2 occurs in string1

#in order to check the working of the function add the following lines to the code:    

first_str = input("Enter the first string: ")  #prompts user to enter first string

second_str = input("Enter the second string (one character long): ") #prompts user to enter second string which should be one character long

occurrence = count_occurrences(first_str,second_str)  #calls count_occurrences method by passing both strings to it

print(second_str,"occurs",occurrence,"times in",first_str) #prints how many times the second_str occurs in the first_str on output screen

Explanation:

The program is well explained in the comments added to each statement of the above code. I will explain the program with an example:

Suppose

string1 = "hello world"

string2 = 'l'

So the method count_occurrences() should return how many times l occurs in hello world.

count is used to count the number of occurrences of l in hello world. It is initialized to 0.

The first loop iterates through each word of string1. The first word of string1 is "hello"

The second loop iterates through each character/letter of each word. So the first letter of "hello" word is 'h'.

The if condition if character == string2: checks if the character is equal to string2 i.e. l.

character = "h"

string2 = "l"

The are not equal so the program moves to the next character of word hello which is e. The if condition again evaluates to false because e is not equal to l. So the program moves to the next character of word hello which is l.

This time the if condition evaluates to true because

character = "l"

string2 = "l"

character == string2

l == l

So the if part is executed which has the following statement:

count = count + 1

So the count is added to 1. count was 0 previously. Now

count = 1

Next the program moves to the next character of word hello which is l. This time the if condition evaluates to true because the character l matches with string2. So count is again incremented to 1. count = 2.

Next the program moves to the next character of word hello which is o. The if condition evaluates to false as o does not match with string2 i.e. l. So the count remains 2.

Next the first loop moves to the next word of the string1 = "world". The inner (second) for loop iterates through each character of "world" from "w" to "d" and checks if any character is equal to string2. Only one character is equal to string2 in "world". So count is incremented to 1. Hence count = 3

After the loop ends the statement return count returns the number of times string2 occurs in the string1. So the output is:

l occurs 3 times in hello world  

You might be interested in
Annie needs to provide a form field that will allow users to select from a predefined list of options but without the ability to
Lana71 [14]

Answer:

Drop down list

Explanation:

You have not listed what the possible answers are, however a drop down list would be an option to accomplish this.

8 0
3 years ago
Given the following method definitions: void mystery(double a) { System.out.print("double! "); } void mystery(int a) { System.ou
kicyunya [14]

Answer:

The correct answer to the following question is option "b".  

Explanation:

The method having the same name but the arguments have different so it is called method overloading. It is a part of the object-oriented programming language (oops).  

  • In the given code we define same method two times that is "mystery()". but in this method, we pass two different parameters. For the first time, we pass a double variable that is "a" as a parameter. In the second time, we pass an integer variable that is "a" as a parameter and in both functions, we write some codes.
  • In the calling time, we call function two times. In first time calling we pass an integer value that is "1" and second time calling we pass double value that is "1.0". So the output of this code is "int! double!".  

That's why the option "b" is correct.

6 0
3 years ago
Why might a peer-to-peer network not be the best choice for a large corporate office setting?
omeli [17]

Answer:

a it is less private

Explanation:

8 0
3 years ago
How do companies use LinkedIn ?
Semenov [28]

Answer:

  1. Introducing new products or services you’ve developed.
  2. Differentiating yourself from your competitors.
  3. Finding job candidates who can make a significant contribution to your business success.
  4. Checking on what your competition is doing.
  5. Improving your ranking in search engines.
8 0
3 years ago
Read 2 more answers
Number of byte required to store illumination
Rina8888 [55]
If we are talking about light that’s an whole different subject
5 0
3 years ago
Other questions:
  • Check My Work _____ are made in response to situations that have occurred frequently enough to enable managers to develop decisi
    15·1 answer
  • A(n) monitoring vulnerability scanner is one that listens in on the network and determines vulnerable versions of both server an
    7·1 answer
  • Composition means ________. a)that data fields should be declared private b)that a class extends another class c)that a variable
    6·1 answer
  • What is the smallest amount of information called?
    13·2 answers
  • Android OS "AFTER" Alpha.<br><br> A. Bubblegum<br><br> B. Beta<br><br> C. Berry<br><br> D. Buzz Cola
    10·1 answer
  • Drag each label to the correct location on the image. Match the movie qualities with the right period of movies.
    8·1 answer
  • A ____ is a a set of presentation rules that control how text should look. It is applied to an XML file to change the collection
    9·1 answer
  • How many total bits are required for a direct-mapped cache with 128 blocks and a block size of 8 bytes, assuming a 4GB main memo
    15·1 answer
  • Every windows service has the 3 start types what are those service types?
    15·1 answer
  • // This pseudocode is intended to describe
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!