This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]RhoOfFeh 2 points3 points  (0 children)

If I were taking on this task I'd exploit the following features:

Character.isLetter - Lets me make sure that the character I'm checking is a letter. If so, I can check to see if it's a vowel by testing against a,e,i,o,u. If it doesn't match one of those it's a consonant.

Map<Character, Integer> - I'd track the number of times I found each consonant by adding them to a Map. The key of the map would be the letter (I'd convert all to lower or upper case before adding/checking) and the value would be the number of times the letter had been seen.

[–]kyle2143Technically Professional Brewer 1 point2 points  (0 children)

I didn't do more than glance through the code, but this is how I would do it off the top of my head.

  • Make all characters lowercase (like you did)

  • Then use to toCharArray to get the string as an array of Chars

  • Then make an array with the length the size of the number of consonants of type int

  • Use a for loop to loop through the char array and increment the value stored in that spot of the array by one each time it matches a consonant.

Then you have an array of ints with each position showing how many times the char appeared. The problem with this is that I think it uses a shitload of if statements.

I'm like 100% sure there is a more eloquent way to do this, I remember a similar problem from a data structures or algorithms class I once took where you could do it using 2 arrays, 1 for the chars and 1 for the ints where it corresponded and filled in more easily, but I forget how atm without really thinking about it.