0% found this document useful (0 votes)
5 views2 pages

Starter Code

The Java program 'SecretCodeGenerator' prompts the user to enter a secret word of at least 5 characters. It then reverses the word, encrypts it by shifting ASCII values, and generates a scrambled pattern of the reversed word. Finally, it displays the final secret code in uppercase along with the reversed word.

Uploaded by

successthecodess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Starter Code

The Java program 'SecretCodeGenerator' prompts the user to enter a secret word of at least 5 characters. It then reverses the word, encrypts it by shifting ASCII values, and generates a scrambled pattern of the reversed word. Finally, it displays the final secret code in uppercase along with the reversed word.

Uploaded by

successthecodess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;

public class SecretCodeGenerator {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);

// Step 1: Get a valid secret word from the user


String secretWord;
do {
System.out.print("Enter a secret word (at least 5 characters): ");
secretWord = input.nextLine();
} while (secretWord.length() < 5); // Input-controlled loop

System.out.println("\nGenerating your secret code...\n");

// Step 2: Reverse the word using a for loop


String reversedWord = "";
for (int i = secretWord.length() - 1; i >= 0; i--) {
reversedWord += secretWord.charAt(i);
}
System.out.println("Step 1: Reversed Word: " + reversedWord);

// Step 3: Encrypt using ASCII shifting


System.out.println("\nStep 2: Encrypting with ASCII shifts...");
System.out.print("Original: ");
for (int i = 0; i < reversedWord.length(); i++) {
System.out.print(reversedWord.charAt(i) + " ");
}

System.out.print("\nEncrypted: ");
String encryptedWord = "";
for (int i = 0; i < reversedWord.length(); i++) {
char shiftedChar = (char) (reversedWord.charAt(i) + 2); // Shift ASCII value
encryptedWord += shiftedChar;
System.out.print(shiftedChar + " ");
}

// Step 4: Generate a pattern using nested loops


System.out.println("\n\nStep 3: Scrambling with a pattern...");
for (int i = 0; i < reversedWord.length(); i++) {
for (int j = 0; j < 3; j++) { // Print each letter 3 times
System.out.print(reversedWord.charAt(i) + " ");
}
System.out.println();
}

// Step 5: Display the final secret code


🔑
System.out.println("\n Your Final Secret Code: " +
encryptedWord.toUpperCase() + "-" + reversedWord);

input.close();
}
}

You might also like