166k views
1 vote
Write a program to find the number of times consonants appear in the string entered by the user. If the user enters the string "hello world", it should print the h:1,l:3, w:1,r:1,d:1. The letters need to be printed in the order they first appeared in the string.

2 Answers

7 votes

Sure, here is a Python program to find the number of times consonants appear in the string entered by the user:

```python

def count_consonants(string):

"""Counts the number of consonants in a string.

Args:

string: The string to count the consonants in.

Returns:

A dictionary mapping each consonant to the number of times it appears in the

string.

"""

consonants = "bcdfghjklmnpqrstvwxyz"

count_dict = {}

for letter in string:

if letter in consonants:

if letter not in count_dict:

count_dict[letter] = 1

else:

count_dict[letter] += 1

return count_dict

def main():

string = input("Enter a string: ")

count_dict = count_consonants(string)

for letter, count in count_dict.items():

print(f"{letter}:{count}")

if __name__ == "__main__":

main()

```

This program first defines a function called `count_consonants()` that takes a string as input and returns a dictionary mapping each consonant to the number of times it appears in the string. The function works by looping through the string and checking if each letter is a consonant. If it is, the function increments the count for that letter in the dictionary.

The main function of the program prompts the user to enter a string and then calls the `count_consonants()` function to count the number of consonants in the string. The function then prints out the results of the count.

To run the program, you can save it as a Python file and then run it from the command line. For example, if you save the program as `count_consonants.py`, you can run it by typing the following command into the command line:

```

python count_consonants.py

```

When you run the program, it will prompt you to enter a string. After you enter a string, the program will print out the number of times each consonant appears in the string.

User GaelS
by
8.9k points
1 vote

Here's a Python program that counts the number of occurrences of consonants in a string entered by the user:

```python

def count_consonants(string):

consonants = []

count = {}

for char in string:

if char.isalpha() and char.lower() not in 'aeiou':

if char.lower() not in consonants:

consonants.append(char.lower())

count[char.lower()] = count.get(char.lower(), 0) + 1

for consonant in consonants:

print(f"{consonant}:{count[consonant]}")

user_input = input("Enter a string: ")

count_consonants(user_input)

```

In this program, the `count_consonants` function takes a string as input. It iterates over each character in the string and checks if it is an alphabetic character and not a vowel. If it satisfies these conditions, it checks if the consonant has been encountered before. If not, it adds the consonant to the `consonants` list. It also updates the count for each consonant in the `count` dictionary.

Finally, the program prints the consonants and their respective counts in the order they first appeared in the string.

Example output:

```

Enter a string: hello world

h:1

l:3

w:1

r:1

d:1

```

Note: The program treats all non-alphabetic characters as non-consonants. If you want to include special characters or spaces in the count, you can modify the condition `char.isalpha()` to suit your requirements.

User Xlson
by
8.2k points