Results 1 to 8 of 8
Thread: help with recursion methods
- 11-05-2010, 06:28 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 45
- Rep Power
- 0
- 11-05-2010, 07:05 PM #2
Well... I'd probably have a method that finds the index of the next vowel. If it's greater than or equal to zero, then increment a counter which is returned from each method. Otherwise, return the existing count and stop recursing. Something like this:
Java Code:String x, "sequoias" int count = find_vowels(x, 0, 0) public int find_vowels(str, index, count) // we need our original string, a starting index to find the NEXT vowel, and a maintained count store nextA, nextE, nextI, nextO, and nextU based on their indices from indexOf find lowest value of nextA, nextE, nextI, nextO, and nextU if (all indices are < 0) return count return find_vowels(str,index+1,count+1) // increment search index and total count
PS: Just put this together with actual code and the concept works.
- 11-06-2010, 05:01 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 45
- Rep Power
- 0
ok that kind of makes sense but the framework required to do this is
Java Code:System.out.println( countVowels( "coconut" ) ); public static int countVowels( String s ) { }
- 11-06-2010, 05:08 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 45
- Rep Power
- 0
i know how to calculate the vowels in a for loop but i cant understand how to do it with recursion here is how i would do it in a loop
Java Code:import java.lang.String; import java.io.*; import java.util.*; public class CountVowels{ public static void main(String args[])throws IOException{ BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the String:"); String text = bf.readLine(); int count = 0; for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') { count++; } } System.out.println("There are" + " " + count + " " + "vowels"); } }
- 11-06-2010, 05:39 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 45
- Rep Power
- 0
i figured it out and used this as my code
Java Code:public static int countVowels( String s ) { if ( s.length() == 0 ) return 0; if ( "aeiouAEIOU".indexOf( s.substring( 0, 1 ) ) < 0 ) return countVowels( s.substring( 1 ) ); else return 1 + countVowels( s.substring( 1 ) ); }
- 11-06-2010, 06:19 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
- 11-16-2012, 05:35 AM #7
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
- 11-16-2012, 05:38 AM #8
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-17-2012, 12:00 AM -
recursion and tail-recursion differences
By OptimusPrime in forum New To JavaReplies: 2Last Post: 12-28-2009, 07:26 PM -
Recursion with static and non static methods
By sh4dyPT in forum New To JavaReplies: 14Last Post: 03-27-2009, 07:56 AM -
help with recursion
By Nari in forum New To JavaReplies: 15Last Post: 04-24-2008, 10:13 AM -
Help With Recursion
By andrew777 in forum New To JavaReplies: 1Last Post: 03-29-2008, 01:51 PM
Bookmarks