Ask Question

Write a function that finds the number of occurrences of a specified character in the string using the following header: def count (s, ch) : For example, count ("Welcome", 'e') returns 2. The str class has the count method. Implement your function without using the count method. Write a test program that reads a string and a character and displays the number of occurrences of the character in the string.

+1
Answers (1)
  1. 5 April, 17:56
    0
    def count (userInput, character):

    occurrences = 0

    for character_in_string in userInput:

    if (character = = character_in_string):

    occurrences + = 1

    return occurrences

    userInput = input ("Enter string: ")

    character = input ("Enter character: ") [0]

    print (count (userInput, character))

    Explanation:

    Define a count function that takes in the userInput as a string and character parameters. Loop through the userInput and check whether selected character is equal to the character found in the string. Increase the counter of occurrences by 1 if the condition is true and then return the occurrences. Take the userInput and character as input from the user. Finally display the results by calling the count function.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a function that finds the number of occurrences of a specified character in the string using the following header: def count (s, ch) ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers