# Thread: help with recursion methods

1. Member
Join Date
Oct 2010
Posts
45
Rep Power
0

## help with recursion methods

Hello for my java programing class i have to do this lab that counts how many vowels are in the word and it can not use loops or *

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.

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 )
{

}```

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{
System.out.println("Enter the String:");
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");
}
}```

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 ) );
}```

6. Originally Posted by hoosierfan24
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 ) );
}```
Well done but change that first substring(0, 1) to charAt(0) (I know, it's a minor nitpick but I am like that ;-)

kind regards,

Jos

7. Member
Join Date
Nov 2012
Posts
2
Rep Power
0

## Re: help with recursion methods

why did you put ( on line 6)
< 0??

and why do you have to have ( on line 7)
s.substring( 1 )

I am just trying to learn, and dont understand.

thank you!

Originally Posted by hoosierfan24
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 ) );
}```

8. Member
Join Date
Nov 2012
Posts
2
Rep Power
0

## Re: help with recursion methods

why do you have on line 6

< 0?

and on line 7

s.substring(1)

sorry just trying to learn

thanks

Originally Posted by JosAH
Well done but change that first substring(0, 1) to charAt(0) (I know, it's a minor nitpick but I am like that ;-)

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•