9.18 LAB: Count characters Write a program 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. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. Ex: If the input is: n Nobody the output is: 0 n is different than N.

Respuesta :

Answer:

# how many char in phrase

# input string of character and phrase

user_string = input(str())

# determine location of character is in phrase

character = user_string[0]

phrase = user_string[1:]

# output # of times character is in phase

if character in phrase:

num_occur = phrase.count(character)

print(num_occur)

if character not in phrase:

print(0)

Explanation:

This is the Zybooks answer in correct format

fichoh

The program counts the number of times a certain alphabet occurs in a particular sentence or word supplied by user. The program is written in python 3 thus :

user_input = input('Enter phrase : ')

#prompts user for input

char_to_count = user_input[0]

#subset the letter to count as it is the first of the input string

word = user_input[1:]

#subset the word

num_counts = word.count(char_to_count)

#count the number of times the alphabet occurs using the count function

if char_to_count in word :

#check if the character to count occurs in the word

print(num_counts)

#display the number of times in occurs

else :

print(0)

#ifit doesn't occur print 0

Learn more : https://brainly.com/question/14942732

Ver imagen fichoh
Q&A Education