// ConlexiaDemoDriver.java — Cross Training: String Manipulation Demo
// Paste into JDoodle (https://www.jdoodle.com/online-java-compiler/) and run.
//
// Concept: iterate a String character by character with charAt(), test each
// one, and accumulate a new String with StringBuilder — the Java idiom for
// efficient string building inside a loop.
//
// String manipulation vocabulary this demo covers:
//   str.charAt(i)              — returns a char primitive (not a String)
//   Character.toLowerCase(c)  — case-converts a char primitive
//   Character.isUpperCase(c)  — case test on a char primitive
//   String.contains(seq)      — membership test; needs CharSequence, so we
//                                convert char to String first
//   str.replace(old, new)     — replaces ALL occurrences (here each letter
//                                appears once, so the effect is the same)
//   StringBuilder.append(c)   — O(1) append; avoids creating a new String
//                                object on every iteration
// ─────────────────────────────────────────────────────────────────────

import java.util.Random;

public class ConlexiaDemoDriver {

    public static String scrambleTargetLetters(
            String message, String targets) {
        StringBuilder result = new StringBuilder();
        Random rng = new Random();

        for (int i = 0; i < message.length(); i++) {
            char symbol   = message.charAt(i);
            char lower    = Character.toLowerCase(symbol);
            String lowerStr = String.valueOf(lower);   // char → String for contains()

            if (targets.contains(lowerStr)) {
                // Remove this letter so we always swap, never keep
                String pool = targets.replace(lowerStr, "");
                char pick = pool.charAt(rng.nextInt(pool.length()));
                // Preserve original case
                result.append(Character.isUpperCase(symbol)
                        ? Character.toUpperCase(pick)
                        : pick);
            } else {
                result.append(symbol);
            }
        }

        return result.toString();
    }

    public static void main(String[] args) {
        String targets = "bdpq";
        String sample  = "Peter Piper picked a peck of pickled peppers " +
                         "quite quickly didn't he, and better than his " +
                         "buddy Bobby Budden!";

        String encrypted = scrambleTargetLetters(sample, targets);
        System.out.println("Original : " + sample);
        System.out.println("Encrypted: " + encrypted);
    }
}
