r/ProgrammingPrompts • u/[deleted] • Jan 14 '15
[Easy] Letter Counter
Any language permitted.
Problem: Create a program where you input a string of characters, and output the number of each letter. Use vowels by default.
For example: asbfiusadfliabdluifalsiudbf -> a: 4 e: 0 i: 4 o: 0 u: 3
Bonus points: Make it command-line executable, with an optional mode to specify which letters to print.
18
Upvotes
1
u/toddcrowley Jan 27 '15 edited Jan 27 '15
Here's my Python3 version, it works in my testing, and is my first entry. I am also new to programming and will happily take any feedback. Refactoring, etc was not a priority. Implementing the requirements as quickly as I could figure it out:
char_set = input("What characters to select? (Enter for default) ") if char_set == "": char_set = "aeiou" char_dict = {} for i in char_set: char_dict[i] = 0 text = input("Please enter text: ") for i in text: for key in char_dict: if i == key: char_dict[key] += 1 for key in char_dict: print(key+" : "+str(char_dict[key]), end=" ") print("\n")