Question

In: Computer Science

Write a program that counts the letters in a given string and then display the letter...

Write a program that counts the letters in a given string and then display the letter count in order from high to low.

The program should:

  • Display a message stating its goal
  • Prompt the user to enter a string input
  • Count the letters in the string (hint: use dictionaries)
  • Display the letter count in order from high to low (sort your letter count)
  • For example, for the input Google the output should be

G - 2

O - 2

E - 1

L - 1

  • Note:
    • Make sure not to count lowercase and uppercase twice (i.e., G an g should be count together)
    • Bonus: if a few letters have the same count, sort the display in an alphabetic order (like in the example)
  • Make sure to include comments that explain all your steps (starts with #). Also use a comment to sign your name at the beginning of the program!

Solutions

Expert Solution

PYTHON CODE:

# your name here

# displaying the description of the program
print('***** Program to count the letters in a given string. *****')
print('***** Then display the letter count in order from high to low. *****\n')

# getting input from the user
inp_str=input('Enter a string: ')

# dictionary to store the character count
char_count={}

# iterating through every character in the string
for c in inp_str:

    # changing the character to uppercase
    c=c.upper()

    # checking the character is in dictionary or not
    if c not in char_count:
        char_count[c]=1
    else:

        # increment the character count by 1
        char_count[c]+=1

# getting list of key,value pairs from the dictionary
lst=list(char_count.items())

# sorting the list by values, if a few letters have the same count,
# sort the display in an alphabetic order
lst.sort(key=lambda x:(-x[1],x[0]))

# displaying the character and count
for k,v in lst:

    print('{0} - {1}'.format(k,v))
  

  


SCREENSHOT FOR CODING:

SCREENSHOT FOR OUTPUT:


Related Solutions

Write a program that accepts a string and character as input, then counts and displays the...
Write a program that accepts a string and character as input, then counts and displays the number of times that character appears (in upper- or lowercase) in the string. Use C++ Enter a string: mallet Enter a character: a "A" appears 1 time(s) Enter a string: Racecar Enter a character: R "R" appears 2 time(s)
Write a program that will read a line of text. Display all the letters that occure...
Write a program that will read a line of text. Display all the letters that occure in the text, one per line and in alphabetical order, along with the number of times each letter occurs in the text. Use an array of base type int of length 26, so that the element at index 0 contains the number of a’s, the element at index 1 contains the number of b’s, and so forth, Alloow both upperCase and lower Case. Define...
Write an 8088/8086 assembly program that counts the length of a null terminated string that starts...
Write an 8088/8086 assembly program that counts the length of a null terminated string that starts at location STR.Assume string length will not exceed 255 character.Print the result on the screen?
use C++ Write a program to calculate the frequency of every letter in an input string...
use C++ Write a program to calculate the frequency of every letter in an input string s. Your program should be able to input a string. Calculate the frequency of each element in the string and store the results Your program should be able to print each letter and its respective frequency. Identify the letter with the highest frequency in your message. Your message should be constructed from at least 50 letters. Example of Output: letters frequency a 10 b...
Suppose that a computer program randomly generates an 8-letter string from the letters A,B,C,D,E. For example,...
Suppose that a computer program randomly generates an 8-letter string from the letters A,B,C,D,E. For example, the program might generate the string CCCCCCCC or DAAEDCBB. The letter in each of the 8 positions is chosen independently of the other positions, and each of the five letters is chosen with equal likelihood. What is the probability that the string contains at least one A or at least one B?
Write a function that counts the colors in a string using JavaScript. String "The quick brown...
Write a function that counts the colors in a string using JavaScript. String "The quick brown fox jumped the blue fence and landed on red paint." This should return the number of colors. The colors you are looking for are "blue, green, brown, gray, black, brown, red, purple".
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a...
IN PYTHON Given a string with duplicate characters in it. Write a program to generate a list that only contains the duplicate characters. In other words, your new list should contain the characters which appear more than once. Suggested Approach Used two nested for loops. The first for loop iterates from 0 to range(len(input_str)). The second for loop iterates from first_loop_index + 1 to range(len(input_str)). The reason you want to start at first_loop_index + 1 in the nested inner loop...
Create program which sorts letters of a string based on ASCII value. The program will then...
Create program which sorts letters of a string based on ASCII value. The program will then print the sorted string to stdout. Use C programming language. - Only use stdio.h - Input prompt should say "Enter string of your choice: " - Remove any newline \n from input string - Implement sorting operation as a function. Should use selection sort algorithm, but you may use a different algorithm - Output should print sorted string on new line Example:     Enter...
Given a string, such as x = ‘itm330’, write a Python program to count the number...
Given a string, such as x = ‘itm330’, write a Python program to count the number of digits and the number of letters in it. For example, in ‘itm330’, there are 3 letters and 3 digits. Hint: Very similar to page 11 on the slides. To check if a character c is a digit, use c.isdigit(). If c is a digit, c.isdigit() will be a True.
write a java program to Translate or Encrypt the given string : (input char is all...
write a java program to Translate or Encrypt the given string : (input char is all in capital letters) { 15 } *) Each character replaced by new character based on its position value in english alphabet. As A is position is 1, and Z is position 26. *) New characters will be formed after skipping the N (position value MOD 10) char forward. A->A+1= B , B->B+2=D ,C->C+3=F, .... Y->Y+(25%10)->Y+5=D A B C D E F G H I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT