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?
Write a program that takes a string from the user, identifies and counts all unique characters...
Write a program that takes a string from the user, identifies and counts all unique characters in that given string. You are bound to use only built-in string functions where necessary. For identification of unique characters and for counting of the characters make separate functions. For character identification Develop a program that takes a string argument, and returns an array containing all unique characters. For character counting Develop a program that takes an array returned from above function as an...
Write a program that takes a string from the user, identifies and counts all unique characters...
Write a program that takes a string from the user, identifies and counts all unique characters in that given string. You are bound to use only built-in string functions where necessary. For identification of unique characters and for counting of the characters make separate functions. For character identification Develop a program that takes a string argument, and returns an array containing all unique characters. For character counting Develop a program that takes an array returned from above function as an...
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.print The result on the screen Assume string length will not exceed 255 character.
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 C++ function to print out all unique letters of a given string. You are...
Write a C++ function to print out all unique letters of a given string. You are free to use any C++ standard library functions and STL data structures and algorithms and your math knowledge here. Extend your function to check whether a given word is an English pangram (Links to an external site.). You may consider your test cases only consist with English alphabetical characters and the character.
Write a program which compresses a given string of 1s and 0s and uncompress the string...
Write a program which compresses a given string of 1s and 0s and uncompress the string that was compressed. Use the run-length compression technique which replaces runs of 0s with a count of how many 0s. The interactive input/output should allow users to select and run required processes. The assignment submission on Blackboard should be as CIS_232_Project_LastName.zip file and include: project report file: ProjectReportLastName.doc program’s source file: ProjectLastName.java program’s bytecode file:   ProjectLastName.class and any other relevant files. The project report...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT