Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. package projects;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class p6 {
  6.  
  7.     public static void main(String[] args) {
  8.         //declare scan as scanner, vowelCount to hold vowel count, newPhrase and removeDoubles to remove same back to back characters
  9.         Scanner scan = new Scanner(System.in);
  10.         int vowelCount = 0;
  11.         String newPhrase ="";
  12.         String removeDoubles = "";
  13.        
  14.         //print header
  15.         System.out.print("Welcome to the disemvoweling utility. \n");
  16.         System.out.print("Enter your phrase: ");
  17.         String phrase = scan.nextLine();  //user enters phrase
  18.        
  19.         //begin loop for phrase length, count values and only add consonants to the newPhrase string
  20.         for(int i=0; i<=(phrase.length()-1); i=i+1){           
  21.             if (isVowel(phrase.charAt(i))){  //function for vowel check below
  22.                 vowelCount = vowelCount + 1;
  23.             }else{
  24.                 newPhrase = newPhrase + phrase.charAt(i);                      
  25.             }                  
  26.         }
  27.        
  28.         //check for duplicates, ran 3 times to check for doubles when its been concatenated
  29.         removeDoubles = newPhrase.replaceAll("(.)\\1", "$1");
  30.         removeDoubles = removeDoubles.replaceAll("(.)\\1", "$1");
  31.         removeDoubles = removeDoubles.replaceAll("(.)\\1", "$1");
  32.         //print the final phrase
  33.         System.out.print("The disemvoweled phrase is: " + removeDoubles);
  34.         //get lengths and percent
  35.         double len = phrase.length();
  36.         double toLen = removeDoubles.length();     
  37.         double redPercent = ((len - toLen) / len * 100);
  38.         //print the final line with #.#% format
  39.         System.out.print("\nReduced from " + len + " to " + toLen + " characters. Reduction rate of ");
  40.         System.out.printf ("%.1f%%", redPercent);
  41.     }
  42.  
  43.     //function to check for vowels, upper and lower case
  44.     static boolean isVowel (char t) {
  45.         return t == 'a' || t == 'e' || t == 'i' || t == 'o' || t == 'u'
  46.                 || t == 'A' || t == 'E' || t == 'I' || t == 'O'
  47.                 || t == 'U';
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement