Write a program to do the following. Ask the user to enter a string. Convert all letters to uppercase. Count and display the frequency of each letter in the string. For example, suppose the user enters the string "Csc.565", the program should display the following output because the letter C appears twice while the letter S appears once:

Respuesta :

Answer:

#section 1

strr=input("Enter a String: ")

allF = {}

strr= strr.upper()

#section 2

for char in strr:

   if char.isalpha():

       if char in allF:

           allF[char] += 1

       else:

           allF[char] = 1

#section 3

a =allF.items()

for key, val in a:

   print(f'{key}  {val}')

Explanation:

#section 1:

prompts the user to enter a string, converts the string to uppercase using the upper()  method and also initializes a dictionary that will be used to sore the each letter and how many time they occur.

i.e. Key(alphabet) : Value(occurrence)

#section 2:

we filter away all none alphabets like numbers and special characters with the isalpha() method, the FOR loop iterates through every character in the string.

if char in allF:

           allF[char] += 1

       else:

           allF[char] = 1

This block of code is responsible  for checking if a character has been stored in the dictionary if it is there it adds to the count in the character  Value in the dictionary  if otherwise(else) it adds to the character to the Key and gives the Value a count of 1.

#section 3:

The items() method returns a view object containing the key-value pairs of the dictionary, as tuples in a list. This allows us to use the for loop to iterate over each pair of Key, Value. and f-string allows us to print the key and value separately and just be more flexible with our output.

check attachment to see implementation of code.

Ver imagen jehonor