Last active
January 7, 2021 15:25
-
-
Save gitaficionado/304b5639ae220482172e5e59d311286e to your computer and use it in GitHub Desktop.
The String Shortener assignment requires students to write a short program in Java to shorten messages by removing duplicate letters and vowels.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class StringShortener { | |
public static void main(String[] args) { | |
//Declare scanner | |
Scanner scan = new Scanner(System.in); | |
//Get message from user input | |
System.out.println("Type the message to be shortened"); | |
String msg = scan.nextLine(); | |
msg = msg.toLowerCase(); | |
String alg1Msg = ""; | |
int repeats = __; | |
int vowels = ___; | |
// Iterate through each letter in msg | |
String ltr = msg.substring(i, i+1); | |
/* The compound statement returns true if ltr is not at the start, | |
* is not preceded by a space, and is a vowel. Short circuit | |
* evaluation prevents an error. | |
*/ | |
{ | |
_________++; | |
} | |
// Increments repeats counter if the character is same as the previous | |
{ | |
________++; | |
} | |
// Adds character to shortened string if neither of the above are true | |
else | |
{ | |
alg1Msg += ltr; | |
} | |
} | |
// Print shortened string and shortening results | |
System.out.println("\nAlgorithm 1"); | |
System.out.println("_________ removed: " + ___________); | |
System.out.println("________________ removed: " + _____________); | |
System.out.println("Algorithm 1 message: " + alg1Msg); | |
System.out.println("Algorithm 1 characters saved: " + (_____.length() - alg1Msg._______())); | |
String alg2Msg = ""; | |
int numLetters = ___; | |
// Iterate through each letter in msg | |
___________________________________{ | |
String ltr = msg.substring(i,i+1); | |
// Only process if this character is not a space | |
if(!ltr.equals(" ")){ | |
/* Check whether this character has already appeared by iterating | |
* through characters from start up to the current letter and using | |
* a boolean flag variable. | |
*/ | |
boolean alreadyUsed = _______; | |
___________________________________{ | |
if(msg.substring(j,j+1).equals(ltr)){ | |
alreadyUsed = ______; | |
} | |
} | |
/* If this character hasn't already appeared, iterate through the | |
* rest of the characters and count how many times it appears. | |
*/ | |
if(!alreadyUsed){ | |
numLetters++; | |
int count = ___; | |
for(int j=i; j<msg.________(); _____){ | |
if(msg.substring(j,j+1).equals(____)){ | |
________; | |
} | |
} | |
alg2Msg += count + ltr; | |
} | |
} | |
} | |
System.out.println("\nAlgorithm 2"); | |
System.out.println("Unique characters found: " + _______________); | |
System.out.println("Algorithm 2 message: " + ________); | |
System.out.println("Algorithm 2 characters saved: " + (msg. _______() - _________.length())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment