Write program to display your name and perform following operations on it: Display n characters from left. (Accept n as input from the user)Count the number of vowels. Reverse it. The code and its output must be explained technically. The explanation can be provided before or after the code, or in the form of comments within the code. The descriptive part of your response must be at least 200 words.
Question
Write program to display your name and perform following operations on it: Display n characters from left. (Accept n as input from the user)Count the number of vowels. Reverse it. The code and its output must be explained technically. The explanation can be provided before or after the code, or in the form of comments within the code. The descriptive part of your response must be at least 200 words.
Solution
Sure! Here is a program that displays your name and performs the requested operations:
# Prompt the user to enter their name
name = input("Enter your name: ")
# Display the name
print("Your name is:", name)
# Prompt the user to enter the number of characters to display from the left
n = int(input("Enter the number of characters to display from the left: "))
# Display n characters from the left
print("Characters from the left:", name[:n])
# Count the number of vowels in the name
vowels = ['a', 'e', 'i', 'o', 'u']
count = 0
for char in name:
if char.lower() in vowels:
count += 1
print("Number of vowels:", count)
# Reverse the name
reversed_name = name[::-1]
print("Reversed name:", reversed_name)
Explanation:
- The program starts by prompting the user to enter their name using the
input()function and stores it in the variablename. - The program then displays the entered name using the
print()function. - Next, the program prompts the user to enter the number of characters they want to display from the left using the
input()function. The entered value is converted to an integer using theint()function and stored in the variablen. - The program uses slicing to extract the first
ncharacters from the name and displays them using theprint()function. - The program initializes a list of vowels and a variable
countto keep track of the number of vowels. - It then iterates over each character in the name using a
forloop and checks if the lowercase version of the character is present in the list of vowels. If it is, thecountvariable is incremented. - Finally, the program displays the count of vowels and the reversed name using the
print()function.
This program allows the user to input their name, specify the number of characters to display from the left, counts the number of vowels in the name, and displays the reversed name.
Similar Questions
Define a class to accept a string and convert it into uppercase. Count and display thenumber of vowels in it.
Read any string and display the vowels
Define a class to accept a string and convert it into uppercase. Count and display thenumber of vowels in it.Input: roboticsOutput: ROBOTICSNumber of vowels: 3
Let name=[“nispal”, “tilak”, “bharat”, “rijesh”, “soniya”]. Display the numbers of vowels in eachname of the given list. Display message like :“nispal contains 2 vowels
Write a program in C to count the number of vowels and consonants in a string. You can use fgets to read the characters.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.