Question

Problem Statement Write a recursive method, vowels, that returns the number of vowels in a string....

Problem Statement
Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method in JAVA

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class CountVowelsRec {
    public static int vowels(String s){
        if(s==null || s.length()==0){
            return 0;
        }
        else{
            char ch = s.charAt(0);
            if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'){
                return 1+vowels(s.substring(1));
            }
            else{
                return vowels(s.substring(1));
            }
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter string: ");
        String s = scan.nextLine();
        System.out.println(vowels(s));
    }
}

Add a comment
Know the answer?
Add Answer to:
Problem Statement Write a recursive method, vowels, that returns the number of vowels in a string....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT