Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AlternatingVowelTest {
- /**
- * Program: AlternatingVowelTest.java by Chris Clarke
- * Date: 17.10.2020
- * For Yahoo! Answers
- */
- /* does string s have alternating vowels and consonants? */
- public static boolean isAlt (String s) {
- final String vowels = "aeiou";
- boolean shouldBeVowelNext = true;
- boolean foundVowel;
- for (int j=0; j<vowels.length(); j++) {
- if (s.charAt(0)==vowels.charAt(j)) {
- shouldBeVowelNext = false;
- break;
- }
- }
- for (int i=1; i<s.length(); i++) {
- foundVowel = false;
- for (int j=0; j<vowels.length(); j++) {
- if (s.charAt(i)==vowels.charAt(j)) {
- foundVowel = true;
- break;
- }
- }
- if (foundVowel != shouldBeVowelNext) return false;
- shouldBeVowelNext = !shouldBeVowelNext;
- }
- return true;
- }
- public static void main (String[] args) {
- String[] testData = {"amazon", "apple", "banana"};
- for (String word: testData) {
- if (isAlt (word)) {
- System.out.println (word + " is alternating between vowels & consonants.");
- } else {
- System.out.println (word + " is not alternating.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement