Ask Question
16 March, 23:13

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. 17 March, 00:42
    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?
Get 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 & 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