Question

Write the code in python programming Language String Statistics: Write a program that reads a string...

Write the code in python programming Language

String Statistics: Write a program that reads a string from the user and displays the following information about the string:

(a) the length of the string,

(b) a histogram detailing the number of occurrences of each vowel in the string (details provided below),

(c) the number of times the first character of the string occurs throughout the entire string,

(d) the number of times the last character of the string occurs throughout the entire string, and

(e) the number of occurrences of digits in the string, i.e. the number of occurrences of any of the characters 0, 1, 2, . . . 9.

Furthermore, your program must contain the following functions:

(a) main: The main function of this program should handle reading the input string from the user and calling the other functions listed below. Additionally, main should be the first function called when the program is executed.

(b) compute_len: This function must accept one string parameter and return the length of the given string.

(c) display_hist: This function must take two arguments: l and c. The first argument should be a string representing a label, and the second argument should be a positive integer representing the frequency of the label. The function should display a histogram with l as the first character on the line, followed by a | and followed by c stars (*). As an example, if the function is called with the following parameters: display_hist(“a”, 5) then it should display the chart:

a | *****

Note: If the function is called with second argument equal to 0, the method should display a histogram without any stars.

Hint: To print multiple strings to the same line of the terminal, you must use the following call to the print function:

print(s, end="")

where s is the string to be printed to the terminal.

This function must be used to produce the histogram of vowel frequencies for the input string. For example, if the user enters the string “queueing" then the histogram produced by your program to the terminal should be:

a |

e | ∗ ∗

i | ∗

o |

u | ∗ ∗

(d) char_count: This method must accept two parameters: a string s and a string c. In your function, you may assume that the length of the string s is at least 1 and the length of the string c is exactly 1. The function must return the number of times the character c occurs in the string s.

Your program must use the above methods to compute the required information about the input string. You will have to call the above methods multiple times in your program with appropriate arguments.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code along with the screenshot:

def char_count(s, c):
count = 0
#count the occurence of character
#by iterating over the string
for sc in s:
if sc == c:
count += 1

return count

def display_hist(l, c):
#print the label
print(l+"|",end="")

#print the required number of stars
for i in range(c):
print("*", end="")
print()

def compute_len(s):
return len(s)

def main():
#take input
inStr = input("Enter the string:")
print("Length is", compute_len(inStr))

#print vowel histogram
vowels = ['a', 'e', 'i', 'o', 'u']
for c in vowels:
display_hist(c, char_count(inStr, c))

#print first and last character count
print("First character count is", char_count(inStr, inStr[0]))
print("Last character count is", char_count(inStr, inStr[-1]))

#print digit count
digits = [str(i) for i in range(10)]
for d in digits:
print("Digit", d, "count is", char_count(inStr, d))

if __name__ == "__main__":
main()

Here are some sample outputs:

Comment in case of any doubts.

Add a comment
Know the answer?
Add Answer to:
Write the code in python programming Language String Statistics: Write a program that reads a string...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create an algorithm for a program that reads a string with a maximum length of 30...

    Create an algorithm for a program that reads a string with a maximum length of 30 from the keyboard. The algorithm should then copy the characters from that string into a second character array in order but only if the character is a vowel (a, e, i, o, u). Once copied, the algorithm should output these values in order. The algorithm should then count and display the number of times each vowel appears in the array. Finally, the algorithm should...

  • Create an algorithm for a program that reads a string with a maximum length of 30 from the keyboard. The algorithm sho...

    Create an algorithm for a program that reads a string with a maximum length of 30 from the keyboard. The algorithm should then copy the characters from that string into a second character array in order but only if the character is a vowel (a, e, i, o, u). Once copied, the algorithm should output these values in order. The algorithm should then count and display the number of times each vowel appears in the array. Finally, the algorithm should...

  • 2. Searching a String: Write a MIPS assembly language program to do the following: Read a...

    2. Searching a String: Write a MIPS assembly language program to do the following: Read a string and store it in memory. Limit the string length to 100 characters. Then, ask the user to enter a character. Search and count the number of occurrences of the character in the string. The search is not case sensitive. Lowercase and uppercase letters should be equal. Then ask the user to enter a string of two characters. Search and count the number of...

  • Write a program(Python language for the following three questions), with comments, to do the following:   ...

    Write a program(Python language for the following three questions), with comments, to do the following:    Ask the user to enter an alphabetic string and assign the input to a variable str1. Print str1. Check if str1 is valid, i.e. consists of only alphabetic characters. If str1 is valid Print “<str1> is a valid string.” If the first character of str1 is uppercase, print the entire string in uppercase, with a suitable message. If the first character of str1 is...

  • c programming

    Write a c program that reads a string with a maximum length of 30 from the keyboard[1]. The program should then copy the characters from that input string into a second character array (in order of input). However, only if a character is a vowel (a, e, i, o, u) should it be copied into the second array. Once copied, the program should output the values stored in the second array (in order of input). The program should then count...

  • Write the following program in MIPS for use in the SPIM simulator. 1) Determine the number...

    Write the following program in MIPS for use in the SPIM simulator. 1) Determine the number of occurrences of a specific character in a string. The character and the string are input by the user. The program should output Character <ch> occurs in string <string> <n> times.

  • Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified...

    Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified character in a string) Write a recursive function that finds the number of occurrences of a specified letter in a string using the following function header. int count(const string& s, char a) For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string and a character, and displays the number of occurrences for the character in the...

  • ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

    ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...

  • Take the following C++ code and add to it the following: Write a program that reads...

    Take the following C++ code and add to it the following: Write a program that reads a string and outputs the number of times each lowercase vowel appears in it. Your program must contain a function with one of its parameters as a char variable, and if the character is a vowel, it increments that vowel's count. #include<iostream> #include<string> using namespace std; int countVowel(char, char); int main(void) { string str1; int countA = 0; int countE = 0; int countI...

  • Write a Java Program that performs the following functionalities: and you can use if statements, cases,...

    Write a Java Program that performs the following functionalities: and you can use if statements, cases, and string methods Enter a new main sentence Find a String Find all incidents of a String Find and Replace a String Replace all the incidents of a String Count the number of words Count a letter’s occurrences Count the total number of letters Delete all the occurrences of a word Exit The program will display a menu with each option above, and then...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT