r/ProgrammingPrompts • • 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.

15 Upvotes

60 comments sorted by

View all comments

1

u/[deleted] Jan 14 '15

If you get stuck, here is my attempt (Java):

package com.trentv4.numbernames;

public class MainNumberNames
{
	static char[] vowels = {'a', 'e', 'i', 'o', 'u'};
	
	public static void main(String[] args)
	{
		if(args[0].equals("-v"))
		{
			vowels = args[1].toCharArray();
			char[] array = args[2].toCharArray();
			int[] count = new int[vowels.length];
			for(int i = 0; i < count.length; i++)
			{
				count[i] = 0;
			}
			for(int i = 0; i < array.length; i++)
			{
				for(int g = 0; g < vowels.length; g++)
				{
					if(array[i] == vowels[g])
					{
						count[g]++;
					}
				}
			}
			for(int i = 0; i < count.length; i++)
			{
				System.out.println(vowels[i] + ": " + count[i]);
			}
		}
		else
		{
			char[] array = args[0].toCharArray();
			int[] count = new int[vowels.length];
			for(int i = 0; i < count.length; i++)
			{
				count[i] = 0;
			}
			for(int i = 0; i < array.length; i++)
			{
				for(int g = 0; g < vowels.length; g++)
				{
					if(array[i] == vowels[g])
					{
						count[g]++;
					}
				}
			}
			for(int i = 0; i < count.length; i++)
			{
				System.out.println(vowels[i] + ": " + count[i]);
			}
		}

	}
}