Unique Punctuation Marks §
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
// caleb b
// unique punctuation marks
Scanner scan = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String sentence = scan.nextLine();
Set<String> uniquePunctuations = new HashSet<>();
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == '.' || sentence.charAt(i) == ',' || sentence.charAt(i) == '?' || sentence.charAt(i) == '!' || sentence.charAt(i) == ';' || sentence.charAt(i) == ':' || sentence.charAt(i) == '-') {
uniquePunctuations.add(sentence.substring(i, i + 1));
}
}
System.out.println("There are " + uniquePunctuations.size() + " unique punctuation marks in the sentence.");
System.out.println("The unique punctuation marks are: " + uniquePunctuations);
}
}
Word Game §
import java.text.DecimalFormat;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main {
public static void main(String[] args) {
// caleb b
// word game
Scanner scan = new Scanner(System.in);
System.out.println("Enter a letter: ");
String letter = scan.nextLine();
letter = letter.toUpperCase();
// remove any extra characters or spaces
letter = letter.replaceAll("[^A-Z]", "");
Set<String> words = new HashSet<String>();
// check if the input is a single letter
if (letter.length() != 1) {
System.out.println("Please enter a single letter.");
System.exit(0);
}
System.out.println("Name as many unique 5 letter words as you can that begin with " + letter + ".");
boolean runGame = true;
while (runGame) {
System.out.println("Enter a 5 letter word that begins with " + letter + ", or type quit to quit: ");
String word = scan.nextLine();
word = word.toUpperCase();
// ensure the word is 5 letters
if (word.length() != 5 && !word.equalsIgnoreCase("quit")) {
System.out.println("Please enter a 5 letter word.");
continue; } else if (word.equalsIgnoreCase("quit")) {
runGame = false;
break; }
// ensure the word begins with the letter
if (!word.startsWith(letter)) {
System.out.println("Please enter a word that begins with " + letter + ".");
continue; }
words.add(word);
}
System.out.print("You got " + words.size() + " words: ");
for (String word : words) {
System.out.print(word + " ");
}
}
}