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.

16 Upvotes

60 comments sorted by

View all comments

1

u/okcodex Jan 14 '15 edited Jan 14 '15

My first entry to one of these things... I wrote it in Java also.

I'm thinking about iterating in a way to make sure that a user can't select to print the same letter twice to cut down on repetition but I'm happy enough with it for now. Turns out it was a SUPER easy fix.

package letterTimes;

import java.util.ArrayList;
import java.util.Scanner;

public class LetterTimes {

    public static void main(String[] args) {
	    Scanner in = new Scanner(System.in);
	
	    String input;
	    String toPrint;
	
	    char temp = 0;
	
	    ArrayList<Character> letters = new ArrayList<Character>();
	    ArrayList<Character> prints = new ArrayList<Character>();
	
	    int[] counters;
	
	    System.out.println("Please enter the string of characters.");
	    input = in.nextLine().toUpperCase();
	    System.out.println("Now please enter the letters you would like to count.");
	    toPrint = in.nextLine().toUpperCase();
	
	    //convert input string to characters and add to arraylist
	    for(int i=0;i < input.length();i++) {
		    temp = input.charAt(i);
		    letters.add(temp);
	    }
	
	    //convert print string to characters and add to arraylist if arraylist doesn't already contain it
	    for(int i=0;i < toPrint.length();i++) {
			    temp = toPrint.charAt(i);
			    if((prints.contains(temp)) || (temp == ' ')){
				
			    }
			    else{
				    prints.add(temp);
			    }
	    }
	
	    counters = new int[prints.size()];
	
	    //loop through arraylists to compare and count if necessary
	    for(int i=0;i < letters.size();i++) {
		    for(int y=0;y < prints.size();y++) {
			    if(prints.get(y) == letters.get(i)) {
				    counters[y]++;
			    }
		    }
	    }
	
	    //print letter and its associated count
	    for(int i=0; i < counters.length; i++ ){
		    System.out.println(prints.get(i) + " : " + counters[i]);
	    }
    }
}

1

u/[deleted] Jan 14 '15

It's a very nice first entry.

1

u/okcodex Jan 14 '15 edited Jan 14 '15

Thanks man. I just ran it a few more times and I'm not happy about a few other things ... for example I should probably .toUpperCase() everything so that it doesn't think "a" and "A" are different... and I should probably ignore spaces... but it's time to move on :P Made the changes anyway because I'm obsessive.

1

u/[deleted] Jan 14 '15

Actually, since you pointed that out I just realized my copy has the same issue. Oh well.

1

u/flushface Jan 15 '15

It would probably be better and easier using a Map<Character, Integer>

1

u/okcodex Jan 15 '15

Ooohhhh I don't know about maps. I'll look into it, thanks.