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
(PLEASE HELP!! I'll award brainiest!!)
Bas_tet [7]

Answer:

Repair

Have a good day

3 0
3 years ago
Read 2 more answers
Documentation for evidence which includes exactly what, when, and from whom evidence was collected, the condition of this eviden
emmasim [6.3K]

Answer:

Answer to the following question is CoC(chain of custody ).

Explanation:

  • CoC(chain of custody ) is the legal document and it is the evidence that is handled during an investigation of the case.
  • In the criminal trials, prosecution must be typically proved that all the evidences is handled according the documented and unbroken CoC.
  • In the court, CoC(chain of custody) documentation has been presented by prosecutions to prove the evidence, related to an alleged crimes, and it has been in possession of  defendant.
4 0
4 years ago
Explain why the cosmic horizon is much smaller than the universe itself
sashaice [31]
The cosmic horizon is what your eye allows you to see, and that is only a small fragment of the universes actual size
3 0
3 years ago
Consider the following code:
pishuonlain [190]

In python the % operator is modulo. Modulo returns the remainder of two numbers.

19 % 5 = 4 therefore,

print(x%y) would output 4

7 0
3 years ago
In the admin console, which three of these gmail settings can an administrator set for the entire domain? (choose 3)
ehidna [41]
<span>In the admin console, three settings than an admin can set for the entire domain in gmail are to enable users to set their own themes in gmail, allow users to delegate their mail out to other users, and enable users to have the ability to import their email from other platforms like yahoo, web mail, or hot mail.</span>
5 0
4 years ago
Other questions:
  • What type of impacts can a computer virus have on a computer?
    12·1 answer
  • Without protocols the information sent and received through the Internet would never reach its intended target and even if it di
    6·1 answer
  • Java Question-5 Declare and initialize two variables called first and second. Write a single statement that will print the messa
    15·1 answer
  • What describes an agreement between two or more parties and demonstrates a "convergence of will" between the parties so that the
    10·1 answer
  • An investment website can tell what devices are used to access the site. The site managers wonder whether they should enhance th
    10·1 answer
  • Technician A says that a lead acid battery uses straight Hydrochloric acid for electrolyte. Technician B says that a lead acid b
    10·2 answers
  • Helpppppppp!!!!!!!! Some program menus are the same in every program you open. Under the File menu, all of the following are the
    15·2 answers
  • Can someone please answer this please!!!
    6·2 answers
  • In cell J6, insert a formula using the OR function that returns TRUE if the rating (cell 16) equals "A" or the email reminder (c
    5·1 answer
  • Write a function to take three integers x, y and n and check if x and y are both fall between 0 to n-1. If yes, your function sh
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!