Ask Question
8 July, 02:22

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.

+4
Answers (1)
  1. 8 July, 03:05
    0
    i = 0

    word = input ('Enter character and phrase: '). lower (). split ()

    phrase = ' '. join (word[1:])

    a = word[0]

    for char in phrase:

    if char = = a:

    i + = 1

    print (i)

    Explanation:

    The programming language used is python.

    we initialize the counter 'i' that will be used to count how many times a letter occurs.

    The program then prompts the user for an input, converts it to lowercase and also, converts it to a list.

    The phrase is extracted using a list slice and converted back to a string using the join function, the the character that is to be scanned for is assigned to a variable.

    Using a FOR loop and an IF statement we check for every character in the phrase and increase the count by one for each time a character appears.

    Finally, the result is printed to the screen.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “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 ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers