Results 1 to 3 of 3
- 09-29-2009, 11:31 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
Is there a way to clean up this recursion code?
Ok, so I wrote a program to count the number of vowels in a user inputted string.
The program works great. However, I was wondering if there was a way to clean my recursion method up, trying to get things as simple as possible if this isn't it. Need to work on cleaning my code up.
Here is the program:
Any advice is much appreciated.Java Code://Program to test vowel count import java.io.*; import java.util.*; public class VowelCount { static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); static int countVowels(String s) { if (s.length() == 0) return 0; int tailResult = countVowels(s.substring(1)); switch (s.charAt(0)) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': return tailResult+1; default: return tailResult; } } public static void main(String[] args) throws IOException { String s; System.out.println("Please enter a string of letters: "); s = keyboard.readLine(); System.out.println("The string you entered is: " + s); countVowels(s); System.out.println("The number of vowels in the string is: " + countVowels(s)); } }
- 09-30-2009, 01:50 AM #2
There are several ways to do this.
Method 1: For each character in the string, check if it's a vowel, and keep a running total.
This method differs from yours in that instead of recursively checking the end of the string, you check each character.Java Code:static int countVowels(String s) { int vowelCount = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == 'a' || c == 'e' || c == 'i' || c=='o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c =='O' || c=='U') { vowelCount++; } } return vowelCount; }
Method 2: Use a regular expression.
I'm a regex maniac, and use it for most of my parsing needs, since it's usually shorter code, and easier to modify.
Java Code:static int countVowels(String s) { int vowelCount = 0; // matches an 'a', 'e', 'i', 'o' or 'u' // (the "(?i)" means case-insensitive (that is, upper-case vowels too) Pattern vowel = Pattern.compile("(?i)[aeiou]"); Matcher matcher = vowel.matcher(s); while (matcher.find()) { vowelCount++; } return vowelCount; }
Also, as a note, this line does nothing, since you don't store the value.
Java Code:countVowels(s);
Instead, you might want to store the value.
Java Code:int vowelCount = countVowels(s); System.out.println("The number of vowels in the string is: " + vowelCount);CodesAway - codesaway.info
writing tools that make writing code a little easier
- 09-30-2009, 02:30 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 80
- Rep Power
- 0
Similar Threads
-
[REQ] could anybody clean this method up for me?
By harryblue in forum Advanced JavaReplies: 5Last Post: 03-19-2009, 02:37 AM -
[REQ] can sombody clean up this code?
By harryblue in forum New To JavaReplies: 1Last Post: 03-18-2009, 08:40 PM -
Recursion
By Mika in forum New To JavaReplies: 5Last Post: 01-04-2009, 01:13 AM -
clean and Build
By bhanu in forum EclipseReplies: 3Last Post: 07-03-2008, 01:13 PM -
Clean the content of the JTextField
By elizabeth in forum AWT / SwingReplies: 1Last Post: 07-26-2007, 08:38 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks