Advertisement
brilliant_moves

AlternatingVowelTest.java

Oct 17th, 2020
3,593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.12 KB | None | 0 0
  1. public class AlternatingVowelTest {
  2.  
  3.     /**
  4.       * Program: AlternatingVowelTest.java by Chris Clarke
  5.       * Date: 17.10.2020
  6.       * For Yahoo! Answers
  7.       */
  8.  
  9.     /* does string s have alternating vowels and consonants? */
  10.  
  11.     public static boolean isAlt (String s) {
  12.         final String vowels = "aeiou";
  13.         boolean shouldBeVowelNext = true;
  14.         boolean foundVowel;
  15.  
  16.         for (int j=0; j<vowels.length(); j++) {
  17.             if (s.charAt(0)==vowels.charAt(j)) {
  18.                 shouldBeVowelNext = false;
  19.                 break;
  20.             }
  21.         }
  22.  
  23.         for (int i=1; i<s.length(); i++) {
  24.             foundVowel = false;
  25.             for (int j=0; j<vowels.length(); j++) {
  26.                 if (s.charAt(i)==vowels.charAt(j)) {
  27.                     foundVowel = true;
  28.                     break;
  29.                 }
  30.             }
  31.             if (foundVowel != shouldBeVowelNext) return false;
  32.             shouldBeVowelNext = !shouldBeVowelNext;
  33.         }
  34.         return true;
  35.     }
  36.  
  37.     public static void main (String[] args) {
  38.         String[] testData = {"amazon", "apple", "banana"};
  39.         for (String word: testData) {
  40.             if (isAlt (word)) {
  41.                 System.out.println (word + " is alternating between vowels & consonants.");
  42.             } else {
  43.                 System.out.println (word + " is not alternating.");
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement