Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package projects;
- import java.util.Scanner;
- public class p6 {
- public static void main(String[] args) {
- //declare scan as scanner, vowelCount to hold vowel count, newPhrase and removeDoubles to remove same back to back characters
- Scanner scan = new Scanner(System.in);
- int vowelCount = 0;
- String newPhrase ="";
- String removeDoubles = "";
- //print header
- System.out.print("Welcome to the disemvoweling utility. \n");
- System.out.print("Enter your phrase: ");
- String phrase = scan.nextLine(); //user enters phrase
- //begin loop for phrase length, count values and only add consonants to the newPhrase string
- for(int i=0; i<=(phrase.length()-1); i=i+1){
- if (isVowel(phrase.charAt(i))){ //function for vowel check below
- vowelCount = vowelCount + 1;
- }else{
- newPhrase = newPhrase + phrase.charAt(i);
- }
- }
- //check for duplicates, ran 3 times to check for doubles when its been concatenated
- removeDoubles = newPhrase.replaceAll("(.)\\1", "$1");
- removeDoubles = removeDoubles.replaceAll("(.)\\1", "$1");
- removeDoubles = removeDoubles.replaceAll("(.)\\1", "$1");
- //print the final phrase
- System.out.print("The disemvoweled phrase is: " + removeDoubles);
- //get lengths and percent
- double len = phrase.length();
- double toLen = removeDoubles.length();
- double redPercent = ((len - toLen) / len * 100);
- //print the final line with #.#% format
- System.out.print("\nReduced from " + len + " to " + toLen + " characters. Reduction rate of ");
- System.out.printf ("%.1f%%", redPercent);
- }
- //function to check for vowels, upper and lower case
- static boolean isVowel (char t) {
- return t == 'a' || t == 'e' || t == 'i' || t == 'o' || t == 'u'
- || t == 'A' || t == 'E' || t == 'I' || t == 'O'
- || t == 'U';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement