CS174: OOP - Drills - Vowel Counting
Developed by Professor Tralie and Professor Mongan.
Exercise Goals
The goals of this exercise are:- To do proper string indexing
- To use conditional statements appropriately
- To keep track of auxiliary variables within loops
countVowels
to count the number of vowels (both lowercase and uppercase, not including y or Y) in a string. Recall that the method charAt
of the String
class returns a character at a particular index, and, like arrays, strings are zero-indexed. For instance, if String s = "I love CS";
s.charAt(3)
o
.Recall also that the length()
method of the String
class returns the total number of characters in the string.
Enter your Ursinus netid before clicking run. This is not your ID number or your email. For example, my netid is ctralie
(non Ursinus students can simply enter their name to get this to run, but they won't get an e-mail record or any form of credit)
Netid |
StringUtils.java
public class StringUtils {
/**
* Count the number of vowels in a string, regardless
* of case
*
* @param s The string to examine
* @return The number of vowels in the string
*/
public static int countVowels(String s) {
int numVowels = 0;
// TODO: Fill this in
return numVowels;
}
}
Tester.java
public class Tester {
public static void main(String[] args) {
String s1 = "I love CS";
int vowels1 = StringUtils.countVowels(s1);
String s2 = "The quick brown fox jumped over the lAzy dOg";
int vowels2 = StringUtils.countVowels(s2);
String s3 = "aEIOu AeioU";
int vowels3 = StringUtils.countVowels(s3);
System.out.print(vowels1 + "." + vowels2 + "." + vowels3);
}
}
Tester.main(null);