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
Which type of sentence error, if any, is the above example? 
sammy [17]
I'm gonna say D. No sentence error
3 0
4 years ago
What organization is responsible for the registration of Internet domain names?
harkovskaia [24]

Answer:

C. Internet Corporation for Assigned Names and Numbers.

Explanation:

The Internet Corporation for Assigned Names and Numbers is an institution founded in September, 1998. The ICANN has the role of maintaining internet databases and promoting safe internet operations. The headquarters of this organization is located in Los Angeles, California.

The ICANN is also involved in the registration of  internet domain names, formulating new top-level domains, as well as the maintenance of root name servers.

The ICANN is also actively involved in forming new policies that would see to safe internet activities. The internet protocol address spaces for IPv4 and 6 are numbered by the ICANN.

5 0
4 years ago
Which of the following might be appropriate to be created as a constant variable?
jasenka [17]

Answer:

Number of feet in a mile

Explanation:

In this problem, we need to find an option that to be created as a constant variable.

In option (a) "number of feet in a mile".

As 1 mile = 5280 foot

The number of feet in a mile is constant in every condition.

In option (b), (c) and (d)

number of people inside a store , current grade in a class , score in a football game are no fixed. It is not created as a constant variable.

Hence, the correct option is (a).

7 0
3 years ago
Choose the correct function to yield the shown result.
HACTEHA [7]

Answer:

int

Explanation:

Correct on edg test 2021

8 0
3 years ago
you+increase+the+size+of+a+computer+screen+display+by+20%20%.+then+you+decrease+it+by+20%20%.+what+is+the+size+of+the+computer+s
emmainna [20.7K]

Answer:

The original size??? +20% and then -20% = 0%. If starting at 100% size the screen would be there.

6 0
2 years ago
Other questions:
  • Earning wise scope of web and mobile app development
    11·1 answer
  • What is the purpose of system calls, and how do system calls relate to the OS and to the concept of dual-mode (kernel-mode and u
    14·1 answer
  • Amazon uses a customer profiling system whenever a customer visits its website. Using this system, Amazon can offer products tai
    8·1 answer
  • Write a program segment that simulates flipping a coin 25 times by generating and displaying 25 random integers, each of which i
    5·1 answer
  • (Bible)<br> Sin may be an inward thought or an outward act. True False
    11·2 answers
  • What is computer science
    6·2 answers
  • Consider an improved version of the Vigen ere cipher, where instead of using multiple shift ciphers, multiple mono-alphabetic su
    13·1 answer
  • What is output? Select all that apply. c = 3 while (c &lt; 10): c = c + 2 print (c)
    6·1 answer
  • PLEASE GET THIS CORRECT I AM TIMED. CORRECT ANSWER GETS BRAINLIEST
    10·2 answers
  • What uses HTML hypertext links that users can click to access different locations or information?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!