173k views
18 votes
Write a program in python whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.

User Bsky
by
3.7k points

1 Answer

8 votes

Answer:

Step-by-step explanation:

The following is written in Python as requested and performs this task with a function called count_appearances. It first makes the character and the phrase into all lowercase and then loops through each word in the phrase and compares it to the character. If they are the same it adds 1 to the counter. Once the loop is over it returns the counter which is the number of times that the character appeared in the phrase.

def count_appearances(character, phrase):

count = 0

character = character.lower()

phrase = phrase.lower()

for x in phrase.split():

if x == character:

count += 1

return count

User Sunil Gouda
by
3.5k points