Ask Question
24 August, 20:39

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.

+5
Answers (1)
  1. 24 August, 23:36
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers